← Back to DevOpsToolbox

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 --version
ansible-doc -l
ansible-inventory -i inventory.ini --list
ansible all -i inventory.ini -m ping
ansible servers -i inventory.ini -m setup
ansible-playbook -i inventory.ini site.yml
ansible-playbook --syntax-check site.yml
ansible-playbook --check --diff site.yml
ansible-playbook site.yml -vvv
ansible-galaxy collection list
ansible-vault view secrets.yml
ansible servers -i inventory.ini -m shell -a 'df -h'

Ansible Command Categories

Jump directly to the Ansible commands you need.

Search Ansible Commands

Search commands by command name, option, module, or description.

109 commands

1. Ansible Information

8 commands
ansible --version

Displays the installed Ansible version and configuration paths.

ansible --help

Displays help for the Ansible ad-hoc command.

ansible-config list

Lists available Ansible configuration settings.

ansible-config dump

Displays the active Ansible configuration values.

ansible-config view

Displays the current ansible.cfg configuration file.

ansible-doc ping

Displays documentation for the ping module.

ansible-doc -l

Lists available Ansible modules.

ansible-doc -s ansible.builtin.copy

Displays the module arguments and usage examples for the copy module.

2. Inventory Management

6 commands
ansible-inventory -i inventory.ini --list

Displays the inventory in JSON format.

ansible-inventory -i inventory.ini --graph

Displays 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-hosts

Lists all hosts that match the selected inventory pattern.

ansible servers -i inventory.ini --list-hosts

Lists hosts belonging to the servers group.

ansible all -i inventory.ini -m ping

Runs a module against all hosts in the specified inventory.

3. Connectivity and Ad-Hoc Commands

8 commands
ansible all -i inventory.ini -m ping

Tests Ansible connectivity to all inventory hosts.

ansible servers -i inventory.ini -m ping

Tests 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 setup

Collects system facts from remote hosts.

ansible servers -i inventory.ini -m gather_facts

Runs 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 commands
ansible 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 commands
ansible-playbook -i inventory.ini site.yml

Runs an Ansible playbook against the specified inventory.

ansible-playbook --syntax-check site.yml

Checks playbook syntax without executing tasks.

ansible-playbook --check site.yml

Runs the playbook in check mode to preview changes.

ansible-playbook --diff site.yml

Shows differences for tasks that modify files.

ansible-playbook -i inventory.ini site.yml -v

Runs a playbook with verbose output.

ansible-playbook -i inventory.ini site.yml -vvv

Runs a playbook with highly detailed debugging output.

ansible-playbook -i inventory.ini site.yml --limit servers

Restricts playbook execution to the selected inventory group or hosts.

ansible-playbook -i inventory.ini site.yml --limit web01

Runs the playbook against a specific host.

6. Tags and Task Control

6 commands
ansible-playbook site.yml --list-tasks

Lists the tasks that would be executed by a playbook.

ansible-playbook site.yml --list-tags

Lists tags available in the playbook.

ansible-playbook site.yml --tags deploy

Runs only tasks matching the specified tag.

ansible-playbook site.yml --skip-tags deploy

Skips tasks matching the specified tag.

ansible-playbook site.yml --start-at-task 'Install Nginx'

Starts playbook execution at a specified task.

ansible-playbook site.yml --step

Runs a playbook interactively, prompting before each task.

7. Variables and Facts

6 commands
ansible 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 commands
ansible-playbook site.yml -e 'deploy=true'

Provides a variable that can be evaluated by task conditions.

ansible servers -i inventory.ini -m setup

Collects facts that can be used by when conditions.

ansible-playbook site.yml --check

Tests conditional task behavior without applying changes.

9. Loops and Repeated Tasks

3 commands
ansible-playbook site.yml --list-tasks

Reviews tasks before executing loop-based automation.

ansible-playbook site.yml --check --diff

Previews changes made by repeated tasks.

ansible-playbook site.yml -vvv

Displays detailed information useful when debugging loops.

10. Handlers

3 commands
ansible-playbook site.yml --list-tasks

Lists tasks and helps verify handler-triggering tasks.

ansible-playbook site.yml --check

Previews playbook execution involving handlers.

ansible-playbook site.yml -vvv

Provides detailed output for handler execution debugging.

11. Privilege Escalation

4 commands
ansible servers -i inventory.ini -b -m command -a 'whoami'

Runs a command with privilege escalation.

ansible-playbook site.yml --become

Enables privilege escalation for a playbook.

ansible-playbook site.yml --ask-become-pass

Prompts 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 commands
ansible-galaxy role list

Lists installed Ansible roles.

ansible-galaxy role install <role>

Installs an Ansible role from Ansible Galaxy or another configured source.

ansible-galaxy collection list

Lists installed Ansible collections.

ansible-galaxy collection install <collection>

Installs an Ansible collection.

ansible-galaxy init myrole

Creates the standard directory structure for a new role.

ansible-galaxy search nginx

Searches Ansible Galaxy for roles matching a keyword.

ansible-galaxy install -r requirements.yml

Installs roles or collections listed in a requirements file.

13. Vault and Secrets

7 commands
ansible-vault create secrets.yml

Creates a new encrypted Ansible Vault file.

ansible-vault view secrets.yml

Displays the decrypted contents of a Vault file.

ansible-vault edit secrets.yml

Edits an encrypted Vault file.

ansible-vault encrypt vars.yml

Encrypts an existing YAML file with Ansible Vault.

ansible-vault decrypt secrets.yml

Decrypts an Ansible Vault file.

ansible-vault rekey secrets.yml

Changes the password used to encrypt a Vault file.

ansible-playbook site.yml --ask-vault-pass

Prompts for the Vault password when running a playbook.

14. SSH and Connection Options

5 commands
ansible servers -i inventory.ini -u ubuntu -m ping

Connects to remote hosts using a specified SSH user.

ansible servers -i inventory.ini --private-key ~/.ssh/id_rsa -m ping

Uses a specific SSH private key for authentication.

ansible servers -i inventory.ini -c local -m ping

Uses the local connection method instead of SSH.

ansible servers -i inventory.ini -m ping -vvvv

Enables very detailed connection debugging output.

ansible servers -i inventory.ini -u ubuntu --become -m ping

Connects using a specific user with privilege escalation enabled.

15. Templates and Configuration Files

3 commands
ansible 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 --diff

Previews configuration changes before deployment.

16. Server Troubleshooting

6 commands
ansible 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 commands
ansible-playbook site.yml -f 10

Sets the number of parallel forks used during playbook execution.

ansible-playbook site.yml --flush-cache

Clears the fact cache before playbook execution when a fact cache is configured.

ansible-playbook site.yml --step

Runs 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 commands
ansible-inventory -i inventory.yml --list

Loads and displays inventory generated from a YAML inventory source.

ansible-inventory -i inventory.yml --graph

Displays groups and hosts from a dynamic inventory source.

ansible all -i inventory.yml --list-hosts

Lists hosts discovered by the inventory source.

19. CI/CD Automation

4 commands
ansible-playbook -i inventory.ini deploy.yml

Runs a deployment playbook from a CI/CD pipeline.

ansible-playbook -i inventory.ini deploy.yml --check --diff

Validates expected deployment changes before applying them.

ansible-playbook -i inventory.ini deploy.yml --limit production

Restricts 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 commands
ansible-inventory -i inventory.ini --graph

Verifies that inventory groups and hosts are structured as expected.

ansible all -i inventory.ini -m ping

Verifies SSH connectivity and Ansible access to hosts.

ansible servers -i inventory.ini -m setup

Verifies that Ansible can collect facts from remote hosts.

ansible-playbook --syntax-check site.yml

Checks the playbook for syntax errors before execution.

ansible-playbook site.yml -vvv

Uses detailed output to investigate task and connection failures.

ansible-playbook site.yml --check --diff

Previews configuration changes and file differences without applying them.

ansible-config dump --only-changed

Displays 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.