ANSIBLE COMMANDS
CHEATSHEET
(A COMPLETE QUICK-REFERENCE GUIDE)
1. Notation & Conventions
<angle-brackets> : placeholders (e.g., <inventory>, <playbook>)
[square-brackets] : optional flags
-i <inventory> : specify inventory file
-m <module> : specify module
-a "<args>" : module arguments
-b : become (sudo)
--check : dry-run (no changes applied)
-v / -vv / -vvv : verbosity levels
2. Basics & Setup
ansible --version – Show Ansible version.
ansible-config dump – Show current configuration.
ansible-config view – View [Link].
ansible-doc -l – List all modules.
ansible-doc <module> – Module documentation.
ansible-inventory --list – Show inventory as JSON.
ansible-inventory --graph – Inventory graph view.
3. Inventory Manage hosts and groups.
ansible all -i hosts --list-hosts – List all hosts.
ansible <group> -i hosts --list-hosts – List group hosts.
ansible all -m ping – Test connectivity.
ansible all -m ping -u <user> – Ping using specific user.
4. Ad-Hoc Commands
Run one-time commands without playbooks
ansible all -m ping – Ping all hosts.
ansible all -m shell -a "uptime" – Run shell command.
ansible all -m command -a "df -h" – Run command module.
ansible all -m copy -a "src=[Link] dest=/tmp/[Link]" – Copy file.
ansible all -m file -a "path=/tmp/test state=touch" – Create file.
ansible all -m yum -a "name=nginx state=present" -b – Install package.
ansible all -m service -a "name=nginx state=started" -b – Start service.
5. Playbooks
Execute automation workflows
ansible-playbook [Link] – Run playbook.
ansible-playbook [Link] -i hosts – Run with inventory.
ansible-playbook [Link] --check – Dry-run.
ansible-playbook [Link] --syntax-check – Syntax check.
ansible-playbook [Link] --list-tasks – List tasks.
ansible-playbook [Link] --list-hosts – List target hosts.
ansible-playbook [Link] -v – Verbose output.
6. Modules – Common
Package Management
yum, dnf, apt – Manage packages.
Files
copy – Copy files.
template – Jinja2 templates.
file – File & directory management.
fetch – Fetch files from remote hosts.
System
service – Manage services.
user – Manage users.
group – Manage groups.
cron – Manage cron jobs.
7. Variables & Facts
Manage dynamic values & Gather system information
ansible all -m setup – Gather facts.
ansible all -m setup -a "filter=ansible_os_family" – Filter facts.
ansible-playbook [Link] -e "var=value" – Extra variables.
ansible-playbook [Link] -e @[Link] – Load vars from file.
8. Roles
Reusable automation components
ansible-galaxy init <role> – Create role structure.
ansible-galaxy role list – List installed roles.
ansible-galaxy role install <role> – Install role.
ansible-galaxy role remove <role> – Remove role.
ansible-galaxy search <keyword> – Search roles.
9. Ansible Galaxy
Community roles & collections
ansible-galaxy collection list – List collections.
ansible-galaxy collection install <name> – Install collection.
ansible-galaxy collection remove <name> – Remove collection.
ansible-galaxy collection search <keyword> – Search collections.
10. Vault (Secure Secrets Management)
ansible-vault create [Link] – Create encrypted file.
ansible-vault edit [Link] – Edit encrypted file.
ansible-vault view [Link] – View encrypted file.
ansible-vault encrypt [Link] – Encrypt file.
ansible-vault decrypt [Link] – Decrypt file.
ansible-playbook [Link] --ask-vault-pass – Run with vault password.
11. Handlers & Tags
Control task execution
ansible-playbook [Link] --tags install – Run specific tags.
ansible-playbook [Link] --skip-tags config – Skip tags.
notify: – Trigger handlers.
handlers: – Define handlers.
12. Debugging & Troubleshooting
Troubleshoot execution issues
ansible-playbook [Link] -vvv – Detailed debug.
ansible all -m debug -a "var=ansible_hostname" – Debug variable.
ansible-playbook [Link] --start-at-task "<task-name>" – Resume from
task.
ansible-playbook [Link] --step – Step-by-step execution.
13. Privilege Escalation
Run tasks with elevated permissions
-b – Enable become (sudo).
--become-user <user> – Run as specific user.
--ask-become-pass – Ask sudo password.
become: true – Enable in playbook.
14. Loops & Conditionals
loop: / with_items: – Loop tasks.
when: – Conditional execution.
register: – Store output in variable.
changed_when: – Override changed status.
failed_when: – Override failure conditions.
15. Cleanup (Manual)
Local cleanup operations.
rm -rf ~/.ansible/tmp – Clear Ansible temp files
rm -rf roles/* – Remove downloaded roles
16. Ansible Workflow (One-Line Summary)
inventory → ping → ad-hoc → playbook → roles → vault → deploy