Tools › Cheat sheets › Containers & DevOps › Ansible
🅰️

Ansible cheat sheet

Ansible automates servers over SSH with no agent. Commands take an inventory (list of hosts) with -i. Always test with --check --diff first.

Click any command to copy it.

Ad-hoc commands

CommandWhat it does
ansible all -i inventory.ini -m pingTest connectivity and Python on all hosts.
ansible web -i inventory.ini -a "uptime"Run a command on the web group.
ansible web -i inventory.ini -b -m apt -a "name=nginx state=present update_cache=yes"Install a package with sudo (become).
ansible web -i inventory.ini -b -m service -a "name=nginx state=restarted"Restart a service.
ansible all -i inventory.ini -m setup -a 'filter=ansible_distribution*'Show OS facts.
ansible all -i inventory.ini -m copy -a 'src=file dest=/tmp/file'Copy a file to every host.

Run playbooks

CommandWhat it does
ansible-playbook -i inventory.ini site.ymlRun a playbook.
ansible-playbook -i inventory.ini site.yml --check --diffDry run showing what would change.
ansible-playbook site.yml --limit web1Run only on one host or group.
ansible-playbook site.yml --tags nginxRun only tasks with a tag.
ansible-playbook site.yml -e "version=1.2.3"Pass an extra variable.
ansible-playbook site.yml --syntax-checkCheck the playbook for syntax errors.
ansible-playbook site.yml --list-tasksList tasks without running them.
ansible-playbook site.yml -vvvVery verbose output for debugging.
ansible-playbook site.yml --start-at-task 'Task name'Resume from a specific task.

Inventory

CommandWhat it does
ansible-inventory -i inventory.ini --listShow the parsed inventory as JSON.
ansible all -i inventory.ini --list-hostsList the hosts that would be targeted.
[web] web1 ansible_host=203.0.113.10 web2 ansible_host=203.0.113.11 [web:vars] ansible_user=deployExample inventory.ini file.
ansible-config dump --only-changedShow settings that differ from the defaults.

Vault (secrets)

CommandWhat it does
ansible-vault create secrets.ymlCreate an encrypted file.
ansible-vault edit secrets.ymlEdit an encrypted file.
ansible-vault encrypt vars.ymlEncrypt an existing file.
ansible-vault decrypt vars.ymlDestructive: Decrypt a file in place, leaving secrets in plain text on disk.
ansible-vault view secrets.ymlShow the contents without saving them.
ansible-playbook site.yml --ask-vault-passPrompt for the vault password when running.

Roles, collections and docs

CommandWhat it does
ansible-galaxy init myroleCreate a role skeleton.
ansible-galaxy install geerlingguy.nginxInstall a role.
ansible-galaxy collection install community.generalInstall a collection.
ansible-doc aptShow documentation for a module.
ansible-doc -l | grep dockerSearch the list of modules.

Playbook skeleton

CommandWhat it does
--- - name: Configure web servers hosts: web become: true tasks: - name: Install nginx ansible.builtin.apt: name: nginx state: present update_cache: true - name: Ensure nginx is running ansible.builtin.service: name: nginx state: started enabled: trueA minimal playbook that installs and starts nginx.

Frequently asked questions

What does --check do?

It simulates the run and reports what would change without changing anything. Combine it with --diff to see file differences.

How do I stop Ansible from asking about SSH host keys?

Add each host to known_hosts first. Disabling host key checking works but removes protection against impersonation.

More Containers & DevOps cheat sheets

🌿 Git🐳 Docker☸️ kubectl🏗️ TerraformAll Containers & DevOps cheat sheets →
Copied to clipboard