Ansible CLI Cheat Sheet
ansible runs one module against a group of hosts (an ad hoc command); ansible-playbook runs a playbook. Both take the same inventory, connection and privilege flags.
Overview
| Task | Command or flag |
|---|---|
| Check every host answers | ansible all -i hosts.yaml -m ansible.builtin.ping |
| Run a playbook | ansible-playbook -i hosts.yaml site.yaml |
| One-off command on a group | ansible workers -i hosts.yaml -b -K -m ansible.builtin.command -a "uptime" |
| Become root on the target | -b (--become) |
| Prompt for the sudo password | -K (--ask-become-pass); without it, a password-protected sudo fails with Missing sudo password |
| Prompt for the SSH password | -k (--ask-pass); needs sshpass on the control machine |
| Only some hosts | --limit k8s-worker-01 |
| Pass a variable | -e "key=value"; extra vars have the highest precedence |
| Preview changes | --check --diff |
| Run notified handlers despite a failure | --force-handlers |
| Start a job and disconnect | -B 1 -P 0 (background, never poll) |
| Which user Ansible really is | ansible <host> -m ansible.builtin.command -a whoami |
Details
Inventory
all:
children:
control_plane:
hosts:
k8s-cp-01:
ansible_host: 192.0.2.11
workers:
hosts:
k8s-worker-01:
ansible_host: 192.0.2.12
vars:
ansible_user: admin
ansible_python_interpreter: /usr/bin/python3 # silences interpreter discovery warnings
Inventory variables beat playbook keywords: see Inventory ansible_user Overrides a Play's remote_user.
First Run on a Fresh Host
A new Debian install accepts only passwords, and Ansible needs SSH to install the key that will replace them. Run the key-distribution playbook once with -k, using the ansible.posix.authorized_key module; every later run uses the key. Before sudo exists, escalate with become_method: su (see Ansible become_method su Runs a Non-Login Shell Without sbin in PATH).
ansible.cfg in the Project
[defaults]
inventory = hosts.yaml
inject_facts_as_vars = False # use ansible_facts['distribution'], not ansible_distribution
local_tmp = /tmp/ansible-local # when the control machine can't write ~/.ansible/tmp
remote_tmp = /tmp/ansible-remote
Ansible copies each module to a temporary directory on the target before running it. UNREACHABLE! ... Failed to create temporary directory on a host you can still SSH into therefore means that host can't write there: a full disk, a read-only root filesystem, or a ~/.ansible owned by root. Async jobs keep their status in ~/.ansible_async; on a read-only root, point them to a RAM-backed /tmp with -e ansible_async_dir=/tmp/.ansible_async.
command and shell Tasks
They always run and always report changed. Add creates:/removes: or a changed_when: condition, or use a module that compares state: see Ansible Reports Every command and shell Task as Changed Unless creates or changed_when Decides and Ansible Handlers Notified in a Failed Run Never Fire, and the Re-run Sees Nothing to Change.
Commands That Cut the Connection
A shutdown or an SSH restart can drop the connection before Ansible reads the result, so a task that succeeded is reported as failed or unreachable. Fire such commands asynchronously and don't wait:
ansible workers -i hosts.yaml -b -m ansible.builtin.shell -a "sleep 2 && shutdown -h now" -B 1 -P 0
Secrets Without Prompts
vars_prompt stops a playbook waiting for input, which breaks make targets and CI. Load secrets from a git-ignored .env and pass them as extra vars:
set -a; source .env; set +a
ansible-playbook playbook.yaml -e "tailscale_auth_key=${TAILSCALE_AUTH_KEY}"
References
- Ansible documentation: Introduction to ad hoc commands - module, arguments and privilege flags.
- Ansible documentation: ansible-playbook - every playbook flag.
- Ansible documentation: Asynchronous actions and polling -
-B,-Pandansible_async_dir. - Ansible documentation: Ansible configuration settings -
ansible.cfgoptions. - DevOps and Infrastructure as Code MOC - hub for automation notes.
- Inventory ansible_user Overrides a Play's remote_user - variable precedence.
- Ansible become_method su Runs a Non-Login Shell Without sbin in PATH - escalating before sudo exists.
- Ansible Reports Every command and shell Task as Changed Unless creates or changed_when Decides - change status of command tasks.
- Ansible Handlers Notified in a Failed Run Never Fire, and the Re-run Sees Nothing to Change -
--force-handlers.