Ansible
Ansible Commands Cheat Sheet
A practical Ansible CLI reference for inventory management, connectivity, modules, playbooks, variables, facts, roles, collections, Vault, templates, troubleshooting and server automation.
Ansible Command Syntax
ansible [pattern] -i [inventory] -m [module] -a [arguments]Use ad-hoc commands for quick operations andansible-playbookfor repeatable automation workflows.
Quick Ansible Command Reference
ansible --versionansible-doc -lansible-inventory -i inventory.ini --listansible all -i inventory.ini -m pingansible servers -i inventory.ini -m setupansible-playbook -i inventory.ini site.ymlansible-playbook --syntax-check site.ymlansible-playbook --check --diff site.ymlansible-playbook site.yml -vvvansible-galaxy collection listansible-vault view secrets.ymlansible servers -i inventory.ini -m shell -a 'df -h'Ansible Command Categories
Jump directly to the Ansible commands you need.
1. Ansible Information
8 commands
View commands →2. Inventory Management
6 commands
View commands →3. Connectivity and Ad-Hoc Commands
8 commands
View commands →4. Common Ansible Modules
8 commands
View commands →5. Playbooks
8 commands
View commands →6. Tags and Task Control
6 commands
View commands →7. Variables and Facts
6 commands
View commands →8. Conditionals
3 commands
View commands →9. Loops and Repeated Tasks
3 commands
View commands →10. Handlers
3 commands
View commands →11. Privilege Escalation
4 commands
View commands →12. Roles and Collections
7 commands
View commands →13. Vault and Secrets
7 commands
View commands →14. SSH and Connection Options
5 commands
View commands →15. Templates and Configuration Files
3 commands
View commands →16. Server Troubleshooting
6 commands
View commands →17. Performance and Execution
4 commands
View commands →18. Dynamic Inventory and Cloud Automation
3 commands
View commands →19. CI/CD Automation
4 commands
View commands →20. Troubleshooting
7 commands
View commands →Search Ansible Commands
Search commands by command name, option, module, or description.
109 commands
1. Ansible Information
8 commandsansible --versionDisplays the installed Ansible version and configuration paths.
ansible --helpDisplays help for the Ansible ad-hoc command.
ansible-config listLists available Ansible configuration settings.
ansible-config dumpDisplays the active Ansible configuration values.
ansible-config viewDisplays the current ansible.cfg configuration file.
ansible-doc pingDisplays documentation for the ping module.
ansible-doc -lLists available Ansible modules.
ansible-doc -s ansible.builtin.copyDisplays the module arguments and usage examples for the copy module.
2. Inventory Management
6 commandsansible-inventory -i inventory.ini --listDisplays the inventory in JSON format.
ansible-inventory -i inventory.ini --graphDisplays inventory groups and hosts as a tree.
ansible-inventory -i inventory.ini --host <host>Displays variables associated with a specific inventory host.
ansible all -i inventory.ini --list-hostsLists all hosts that match the selected inventory pattern.
ansible servers -i inventory.ini --list-hostsLists hosts belonging to the servers group.
ansible all -i inventory.ini -m pingRuns a module against all hosts in the specified inventory.
3. Connectivity and Ad-Hoc Commands
8 commandsansible all -i inventory.ini -m pingTests Ansible connectivity to all inventory hosts.
ansible servers -i inventory.ini -m pingTests connectivity to hosts in the servers group.
ansible servers -i inventory.ini -m command -a 'uptime'Runs a command on remote servers using the command module.
ansible servers -i inventory.ini -m shell -a 'df -h'Runs a shell command on remote servers.
ansible servers -i inventory.ini -m setupCollects system facts from remote hosts.
ansible servers -i inventory.ini -m gather_factsRuns fact gathering against the selected hosts.
ansible servers -i inventory.ini -m command -a 'hostname'Displays the hostname of remote servers.
ansible servers -i inventory.ini -m command -a 'whoami'Displays the remote execution user.
4. Common Ansible Modules
8 commandsansible servers -i inventory.ini -m package -a 'name=nginx state=present'Installs a package using the package module.
ansible servers -i inventory.ini -m service -a 'name=nginx state=started'Starts or manages a service.
ansible servers -i inventory.ini -m file -a 'path=/tmp/test state=touch'Creates or manages files and directories.
ansible servers -i inventory.ini -m copy -a 'src=app.conf dest=/etc/app.conf'Copies a file from the control node to remote hosts.
ansible servers -i inventory.ini -m template -a 'src=app.conf.j2 dest=/etc/app.conf'Renders a Jinja2 template and copies it to remote hosts.
ansible servers -i inventory.ini -m command -a 'whoami'Runs a command without shell-specific processing.
ansible servers -i inventory.ini -m shell -a 'echo $HOME'Runs a command through the remote shell.
ansible servers -i inventory.ini -m debug -a 'msg="Hello Ansible"'Prints a debugging message during execution.
5. Playbooks
8 commandsansible-playbook -i inventory.ini site.ymlRuns an Ansible playbook against the specified inventory.
ansible-playbook --syntax-check site.ymlChecks playbook syntax without executing tasks.
ansible-playbook --check site.ymlRuns the playbook in check mode to preview changes.
ansible-playbook --diff site.ymlShows differences for tasks that modify files.
ansible-playbook -i inventory.ini site.yml -vRuns a playbook with verbose output.
ansible-playbook -i inventory.ini site.yml -vvvRuns a playbook with highly detailed debugging output.
ansible-playbook -i inventory.ini site.yml --limit serversRestricts playbook execution to the selected inventory group or hosts.
ansible-playbook -i inventory.ini site.yml --limit web01Runs the playbook against a specific host.
7. Variables and Facts
6 commandsansible servers -i inventory.ini -m debug -a 'var=hostvars[inventory_hostname]'Displays host variables available to the selected host.
ansible servers -i inventory.ini -m debug -a 'var=ansible_facts'Displays gathered Ansible facts.
ansible-playbook site.yml -e 'app_env=production'Passes an extra variable to a playbook.
ansible-playbook site.yml -e '@vars.yml'Loads extra variables from a YAML file.
ansible servers -i inventory.ini -m debug -a 'var=ansible_hostname'Displays the hostname gathered by Ansible facts.
ansible servers -i inventory.ini -m debug -a 'var=ansible_distribution'Displays the remote operating system distribution.
8. Conditionals
3 commandsansible-playbook site.yml -e 'deploy=true'Provides a variable that can be evaluated by task conditions.
ansible servers -i inventory.ini -m setupCollects facts that can be used by when conditions.
ansible-playbook site.yml --checkTests conditional task behavior without applying changes.
9. Loops and Repeated Tasks
3 commandsansible-playbook site.yml --list-tasksReviews tasks before executing loop-based automation.
ansible-playbook site.yml --check --diffPreviews changes made by repeated tasks.
ansible-playbook site.yml -vvvDisplays detailed information useful when debugging loops.
10. Handlers
3 commandsansible-playbook site.yml --list-tasksLists tasks and helps verify handler-triggering tasks.
ansible-playbook site.yml --checkPreviews playbook execution involving handlers.
ansible-playbook site.yml -vvvProvides detailed output for handler execution debugging.
11. Privilege Escalation
4 commandsansible servers -i inventory.ini -b -m command -a 'whoami'Runs a command with privilege escalation.
ansible-playbook site.yml --becomeEnables privilege escalation for a playbook.
ansible-playbook site.yml --ask-become-passPrompts for the privilege escalation password.
ansible servers -i inventory.ini -b -m shell -a 'systemctl status nginx'Runs a privileged service-management command.
12. Roles and Collections
7 commandsansible-galaxy role listLists installed Ansible roles.
ansible-galaxy role install <role>Installs an Ansible role from Ansible Galaxy or another configured source.
ansible-galaxy collection listLists installed Ansible collections.
ansible-galaxy collection install <collection>Installs an Ansible collection.
ansible-galaxy init myroleCreates the standard directory structure for a new role.
ansible-galaxy search nginxSearches Ansible Galaxy for roles matching a keyword.
ansible-galaxy install -r requirements.ymlInstalls roles or collections listed in a requirements file.
13. Vault and Secrets
7 commandsansible-vault create secrets.ymlCreates a new encrypted Ansible Vault file.
ansible-vault view secrets.ymlDisplays the decrypted contents of a Vault file.
ansible-vault edit secrets.ymlEdits an encrypted Vault file.
ansible-vault encrypt vars.ymlEncrypts an existing YAML file with Ansible Vault.
ansible-vault decrypt secrets.ymlDecrypts an Ansible Vault file.
ansible-vault rekey secrets.ymlChanges the password used to encrypt a Vault file.
ansible-playbook site.yml --ask-vault-passPrompts for the Vault password when running a playbook.
14. SSH and Connection Options
5 commandsansible servers -i inventory.ini -u ubuntu -m pingConnects to remote hosts using a specified SSH user.
ansible servers -i inventory.ini --private-key ~/.ssh/id_rsa -m pingUses a specific SSH private key for authentication.
ansible servers -i inventory.ini -c local -m pingUses the local connection method instead of SSH.
ansible servers -i inventory.ini -m ping -vvvvEnables very detailed connection debugging output.
ansible servers -i inventory.ini -u ubuntu --become -m pingConnects using a specific user with privilege escalation enabled.
15. Templates and Configuration Files
3 commandsansible servers -i inventory.ini -m template -a 'src=app.conf.j2 dest=/etc/app.conf'Renders a Jinja2 template and deploys the resulting file.
ansible servers -i inventory.ini -m copy -a 'src=app.conf dest=/etc/app.conf'Copies a static configuration file to remote hosts.
ansible-playbook site.yml --check --diffPreviews configuration changes before deployment.
16. Server Troubleshooting
6 commandsansible servers -i inventory.ini -m shell -a 'uptime'Checks system uptime on remote servers.
ansible servers -i inventory.ini -m shell -a 'free -h'Checks memory usage on remote servers.
ansible servers -i inventory.ini -m shell -a 'df -h /'Checks root filesystem disk usage.
ansible servers -i inventory.ini -m shell -a 'systemctl --failed'Lists failed systemd services.
ansible servers -i inventory.ini -m shell -a 'ss -tulpn'Displays listening TCP and UDP ports.
ansible servers -i inventory.ini -m shell -a 'journalctl -p err -n 50'Displays recent high-priority system logs.
17. Performance and Execution
4 commandsansible-playbook site.yml -f 10Sets the number of parallel forks used during playbook execution.
ansible-playbook site.yml --flush-cacheClears the fact cache before playbook execution when a fact cache is configured.
ansible-playbook site.yml --stepRuns a playbook interactively, prompting before each task.
ansible-playbook site.yml --start-at-task 'Deploy application'Starts execution at a specific task.
18. Dynamic Inventory and Cloud Automation
3 commandsansible-inventory -i inventory.yml --listLoads and displays inventory generated from a YAML inventory source.
ansible-inventory -i inventory.yml --graphDisplays groups and hosts from a dynamic inventory source.
ansible all -i inventory.yml --list-hostsLists hosts discovered by the inventory source.
19. CI/CD Automation
4 commandsansible-playbook -i inventory.ini deploy.ymlRuns a deployment playbook from a CI/CD pipeline.
ansible-playbook -i inventory.ini deploy.yml --check --diffValidates expected deployment changes before applying them.
ansible-playbook -i inventory.ini deploy.yml --limit productionRestricts automated deployment to the production inventory group.
ansible-playbook -i inventory.ini deploy.yml -e 'image_tag=latest'Passes a deployment variable from a CI/CD workflow.
20. Troubleshooting
7 commandsansible-inventory -i inventory.ini --graphVerifies that inventory groups and hosts are structured as expected.
ansible all -i inventory.ini -m pingVerifies SSH connectivity and Ansible access to hosts.
ansible servers -i inventory.ini -m setupVerifies that Ansible can collect facts from remote hosts.
ansible-playbook --syntax-check site.ymlChecks the playbook for syntax errors before execution.
ansible-playbook site.yml -vvvUses detailed output to investigate task and connection failures.
ansible-playbook site.yml --check --diffPreviews configuration changes and file differences without applying them.
ansible-config dump --only-changedDisplays only Ansible configuration values that differ from defaults.
Ansible Troubleshooting Workflow
# Check Ansible installation ansible --version # Check available modules ansible-doc -l # Check inventory ansible-inventory -i inventory.ini --graph # Check a specific host ansible-inventory -i inventory.ini --host web01 # Test connectivity ansible all -i inventory.ini -m ping # Check server facts ansible servers -i inventory.ini -m setup # Check disk usage ansible servers -i inventory.ini -m shell -a "df -h" # Check memory ansible servers -i inventory.ini -m shell -a "free -h" # Check failed services ansible servers -i inventory.ini -m shell -a "systemctl --failed" # Check listening ports ansible servers -i inventory.ini -m shell -a "ss -tulpn" # Check playbook syntax ansible-playbook --syntax-check site.yml # Preview changes ansible-playbook site.yml --check --diff # Run with detailed output ansible-playbook site.yml -vvv
Start with the inventory andpingmodule. If connectivity works, inspect facts, permissions, variables and playbook syntax before troubleshooting individual tasks.
DevOpsToolbox Tip
For safer automation, validate a playbook with--syntax-checkfirst, then use--check --diffto preview changes before applying them to servers.