Ansible Cheat Sheet¶
Quick-reference guide for Ansible ad-hoc commands, playbooks, and command-line syntax.
Ansible Ad-Hoc Commands¶
Ping all hosts:
Ping with custom inventory:
Check uptime:
Install a package:
Restart a service:
Copy a file:
Gather facts:
Playbook Management¶
Run a playbook:
Run with custom inventory:
Check for syntax errors:
Dry run (Check mode):
Run specific tags:
Skip specific tags:
Limit to one host/group:
Step-by-step execution:
Ansible Vault (Secrets)¶
Create encrypted file:
Encrypt existing file:
Decrypt a file:
Edit encrypted file:
Run with vault pass prompt:
Run with vault pass file:
Inventory & Roles¶
List hosts in a group:
Create a new role:
Install role from Galaxy:
Install roles from requirements:
List installed roles:
Useful Variables & Debugging¶
- Debug a variable:
- Common Magic Variables:
{{ inventory_hostname }}: The name of the current host as defined in the inventory.{{ groups['webservers'] }}: List of all hosts in the 'webservers' group.{{ ansible_default_ipv4.address }}: The primary IP of the managed node (requires fact-gathering).{{ hostvars['hostname']['var_name'] }}: Access variables assigned to another host.
Configuration Tips¶
- The
ansible.cfgfile: Ansible evaluates configuration files in the following order of precedence: ANSIBLE_CONFIG(environment variable)./ansible.cfg(current directory)~/.ansible.cfg(user home directory)/etc/ansible/ansible.cfg(default)- Privilege Escalation (Become):
- Use
--become(or-b) to run operations with privileges (default sudo). - Use
-K(or--ask-become-pass) in the CLI to prompt for the sudo password. - Use
--become-user [user]to escalate to a user other than root.