Ansible is an open-source automation tool that simplifies the process of managing and deploying software applications. It is designed to automate repetitive tasks, such as configuration management, application deployment, and infrastructure provisioning. Ansible is easy to use, flexible, and scalable, making it a popular choice for DevOps teams.
One of the key benefits of Ansible is its simplicity. It uses a simple YAML-based language to define tasks and playbooks, which makes it easy to learn and use. Ansible also has a large and active community, which provides support, documentation, and a wide range of pre-built modules and plugins.
This cheat sheet provides an overview of the most commonly used Ansible commands and modules. For more detailed information, please refer to the official Ansible documentation.
Table of Contents
Getting Started
Command Description ansible --version
Display Ansible version ansible all -m ping
Test connectivity to all hosts ansible <host> -m ping
Test connectivity to a specific host ansible all -a ""<command>""
Run a command on all hosts ansible <host> -a ""<command>""
Run a command on a specific host ansible-doc <module>
Display documentation for a specific module
Inventory
Command Description ansible-inventory --list
Display inventory in JSON format ansible-inventory --graph
Display inventory in graph format ansible-inventory --host <host>
Display information about a specific host ansible-inventory --list --yaml
Display inventory in YAML format ansible-inventory --list --toml
Display inventory in TOML format
Playbooks
Command Description ansible-playbook <playbook.yml>
Run a playbook ansible-playbook <playbook.yml> --check
Dry run a playbook ansible-playbook <playbook.yml> --tags <tag>
Run a specific tag in a playbook ansible-playbook <playbook.yml> --skip-tags <tag>
Skip a specific tag in a playbook ansible-playbook <playbook.yml> --start-at-task <task>
Start a playbook at a specific task ansible-playbook <playbook.yml> --step
Prompt for confirmation before each task
Modules
Module Description apt
Manage packages on Debian-based systems yum
Manage packages on Red Hat-based systems file
Manage files and directories copy
Copy files to remote hosts template
Copy files with variable substitution lineinfile
Manage lines in files service
Manage services user
Manage users and groups group
Manage groups command
Run commands on remote hosts shell
Run shell commands on remote hosts raw
Run raw commands on remote hosts script
Run scripts on remote hosts wait_for
Wait for a condition to be true assert
Assert a condition is true debug
Print debug information
Variables
Command Description vars
Define variables in a playbook set_fact
Set a fact for a host register
Store the result of a command in a variable hostvars
Access variables for a specific host group_vars
Define variables for a group of hosts host_vars
Define variables for a specific host ansible_facts
Access facts about a host
Conditionals
Command Description when
Conditionally execute a task failed_when
Define a task as failed based on a condition changed_when
Define a task as changed based on a condition ignore_errors
Ignore errors and continue with the playbook no_log
Do not log sensitive information
Loops
Command Description with_items
Loop over a list of items loop
Loop over a list of items until
Loop until a condition is true retries
Retry a task a specified number of times delay
Add a delay between retries
Handlers
Command Description handlers
Define a handler in a playbook notify
Trigger a handler
Roles
Command Description ansible-galaxy init <role>
Create a new role roles
Include a role in a playbook include_role
Include a role in a playbook
Debugging
Command Description ansible-playbook <playbook.yml> -vvv
Increase verbosity ansible-playbook <playbook.yml> --syntax-check
Check syntax of a playbook ansible-playbook <playbook.yml> --list-tasks
List tasks in a playbook ansible-playbook <playbook.yml> --list-hosts
List hosts in a playbook ansible-playbook <playbook.yml> --list-tags
List tags in a playbook ansible-playbook <playbook.yml> --diff
Show differences when copying files
References