Ansible Cheat Sheet

Agentless configuration management over SSH — ad-hoc commands, playbooks, inventories, and roles.

Infrastructure as Code4 sections · 26 commands
⌘K

Ad-Hoc Commands

7
ansible all -m ping

Test connectivity to all hosts

ansible all -m apt -a 'name=nginx state=present' -b

Install a package with sudo

ansible webservers -m command -a uptime

Run a shell command

ansible all -m copy -a 'src=x dest=/tmp/x'

Copy a file to hosts

ansible all -m service -a 'name=ssh state=restarted' -b

Restart a service

ansible all -a 'tail /var/log/auth.log'

Shortcut: run as command module

ansible all --list-hosts

List the target hosts

Playbooks

10
ansible-playbook site.yml

Run a playbook

ansible-playbook site.yml --check

Dry-run (check mode)

ansible-playbook site.yml --diff

Show file changes

ansible-playbook site.yml -l webservers

Limit to a group

ansible-playbook site.yml --tags install

Run tagged tasks only

ansible-playbook site.yml --start-at-task 'X'

Resume from a task

ansible-playbook site.yml --syntax-check

Validate the playbook

ansible-playbook site.yml --list-tasks

List all tasks

ansible-playbook site.yml --step

Confirm each task

ansible-playbook site.yml -e 'env=prod'

Override a variable

Inventories

4
ansible -i inventory/prod.hosts all -m ping

Use a specific inventory

ansible-inventory -i hosts --list

Dump the resolved inventory

ansible-inventory -i hosts --graph

Group hierarchy as text

ansible all -m ping --vault-password-file .vault

Decrypt vault vars

Roles & Galaxy

5
ansible-galaxy init myrole

Scaffold a role

ansible-galaxy install geerlingguy.nginx

Install a community role

ansible-galaxy install -r requirements.yml

Install roles from file

ansible-galaxy role list

Installed roles

ansible-config dump | grep -i forks

Tune parallel forks