Ansible Cheat Sheet

In YAML

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

CommandDescription
ansible --versionDisplay Ansible version
ansible all -m pingTest connectivity to all hosts
ansible <host> -m pingTest 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

CommandDescription
ansible-inventory --listDisplay inventory in JSON format
ansible-inventory --graphDisplay inventory in graph format
ansible-inventory --host <host>Display information about a specific host
ansible-inventory --list --yamlDisplay inventory in YAML format
ansible-inventory --list --tomlDisplay inventory in TOML format

Playbooks

CommandDescription
ansible-playbook <playbook.yml>Run a playbook
ansible-playbook <playbook.yml> --checkDry 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> --stepPrompt for confirmation before each task

Modules

ModuleDescription
aptManage packages on Debian-based systems
yumManage packages on Red Hat-based systems
fileManage files and directories
copyCopy files to remote hosts
templateCopy files with variable substitution
lineinfileManage lines in files
serviceManage services
userManage users and groups
groupManage groups
commandRun commands on remote hosts
shellRun shell commands on remote hosts
rawRun raw commands on remote hosts
scriptRun scripts on remote hosts
wait_forWait for a condition to be true
assertAssert a condition is true
debugPrint debug information

Variables

CommandDescription
varsDefine variables in a playbook
set_factSet a fact for a host
registerStore the result of a command in a variable
hostvarsAccess variables for a specific host
group_varsDefine variables for a group of hosts
host_varsDefine variables for a specific host
ansible_factsAccess facts about a host

Conditionals

CommandDescription
whenConditionally execute a task
failed_whenDefine a task as failed based on a condition
changed_whenDefine a task as changed based on a condition
ignore_errorsIgnore errors and continue with the playbook
no_logDo not log sensitive information

Loops

CommandDescription
with_itemsLoop over a list of items
loopLoop over a list of items
untilLoop until a condition is true
retriesRetry a task a specified number of times
delayAdd a delay between retries

Handlers

CommandDescription
handlersDefine a handler in a playbook
notifyTrigger a handler

Roles

CommandDescription
ansible-galaxy init <role>Create a new role
rolesInclude a role in a playbook
include_roleInclude a role in a playbook

Debugging

CommandDescription
ansible-playbook <playbook.yml> -vvvIncrease verbosity
ansible-playbook <playbook.yml> --syntax-checkCheck syntax of a playbook
ansible-playbook <playbook.yml> --list-tasksList tasks in a playbook
ansible-playbook <playbook.yml> --list-hostsList hosts in a playbook
ansible-playbook <playbook.yml> --list-tagsList tags in a playbook
ansible-playbook <playbook.yml> --diffShow differences when copying files

References