DevOpsil
Ansible28 commands

Ansible Cheat Sheet

Essential Ansible commands for automation — playbooks, inventory, roles, vault, and ad-hoc commands.

Ad-Hoc Commands

CommandDescription
ansible all -m ping
Ping all hosts
ansible all -m setup
Gather facts from all hosts
ansible all -a "uptime"
Run shell command on all hosts
ansible webservers -m copy -a "src=f.txt dest=/tmp/"
Copy file to group
ansible all -m apt -a "name=nginx state=present" -b
Install package (Debian)
ansible all -m service -a "name=nginx state=started" -b
Start a service

Playbooks

CommandDescription
ansible-playbook site.yml
Run a playbook
ansible-playbook site.yml -i inventory.ini
Run with custom inventory
ansible-playbook site.yml --limit webservers
Limit to host group
ansible-playbook site.yml --tags deploy
Run only tagged tasks
ansible-playbook site.yml --skip-tags debug
Skip tagged tasks
ansible-playbook site.yml --check
Dry run (check mode)
ansible-playbook site.yml --diff
Show file changes
ansible-playbook site.yml -e "env=prod"
Pass extra variables

Inventory

CommandDescription
ansible-inventory --list
List all inventory hosts as JSON
ansible-inventory --graph
Show inventory tree
ansible all --list-hosts
List matched hosts
ansible-inventory -i hosts.yml --host <name>
Show host variables

Vault

CommandDescription
ansible-vault create secrets.yml
Create encrypted file
ansible-vault edit secrets.yml
Edit encrypted file
ansible-vault encrypt secrets.yml
Encrypt existing file
ansible-vault decrypt secrets.yml
Decrypt file
ansible-vault rekey secrets.yml
Change vault password
ansible-playbook site.yml --ask-vault-pass
Run with vault prompt

Roles & Galaxy

CommandDescription
ansible-galaxy init <role_name>
Scaffold a new role
ansible-galaxy install <role>
Install role from Galaxy
ansible-galaxy list
List installed roles
ansible-galaxy collection install <ns.collection>
Install collection