Skip to content

Instantly share code, notes, and snippets.

@mrjk
Last active March 28, 2024 15:06
Show Gist options
  • Save mrjk/1a2bf75e686079fa87614461218f3b73 to your computer and use it in GitHub Desktop.
Save mrjk/1a2bf75e686079fa87614461218f3b73 to your computer and use it in GitHub Desktop.
Ansible cheatsheet

Ansible Cheat Sheet

Installation

When working on complex project, always install your own version of ansible in a dedicated virtual environment:

virtualenv -p python3 .venv
. .venv/bin/activate

Normally, you have at least a requirements.txt file determining python dependencies versions:

ansible==8.7.0
jmespath==1.0.1
zabbix-api==0.5.6

So to install the whoe thing:

pip install -r requirements.txt

Basic ansible config

Basic ansible.cfg:

[defaults]
inventory = inventories

# Speed tuning
pipelining = True
forks=15

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s

Discovery comamnds:

ansible-inventory --graph
ansible-playbook --check --diff site.yml 

Secret management (with direnv)

Direnv .envrc config would looks like:

layout python3

export ANSIBLE_VAULT_PASSWORD_FILE=$PWD/.vault.secret
export ANSIBLE_BECOME_PASSWORD_FILE=$PWD/.become.secret

Create both file, and paste your passwords in clear text without line returns. You should not need anymore to enter passwords.

Troubleshooting and debug

Show inventory vars for a sinble host:

# ansible-inventory --list
ansible-inventory --host HOST
{
    "always_pull": false,
    "cert_key": "wildcard.domain.key",
    "certificate": "wildcard.domain.crt"
}

Show facts only:

ansible -m setup HOST

Show runtime vars for a sinble host:

ansible -m debug -a "var=hostvars[inventory_hostname]" HOST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment