Skip to main content

Ansible Cheatsheet

By Dejan Panovski Updated on Download PDF

Quick reference for Ansible commands, inventories, playbooks, modules, and vault

Ansible is an agentless automation tool that configures servers over SSH using YAML playbooks. This cheatsheet covers the commands, inventory syntax, ad hoc usage, common modules, and vault options you reach for most often.

Install & Verify

Set up Ansible on the control node. See the full Ansible install guide .

CommandDescription
sudo apt install ansibleInstall on Ubuntu, Debian, and derivatives
sudo dnf install ansibleInstall on Fedora, RHEL, and derivatives
pipx install --include-deps ansibleInstall latest in an isolated environment
ansible --versionShow version and config file path
ansible-config dumpShow all active configuration settings
ansible-config dump --only-changedShow only non-default settings

Inventory

Define and inspect the hosts Ansible manages.

CommandDescription
ansible-inventory --listShow the full inventory as JSON
ansible-inventory --graphShow groups and hosts as a tree
ansible all --list-hostsList every host in the inventory
ansible web --list-hostsList hosts in the web group
ansible -i inventory.ini all -m pingUse a specific inventory file
ansible -i 'host,' all -m pingUse an inline inventory (note the comma)

Inventory File (INI)

Basic structure of a static inventory.ini file.

SyntaxDescription
[web]Define a host group
web1 ansible_host=192.168.1.21Host alias with connection address
[all:vars]Variables applied to every host
ansible_user=ubuntuSSH user for the connection
ansible_python_interpreter=/usr/bin/python3Pin the remote Python path
[prod:children]Group made up of other groups

Ad Hoc Commands

Run a single module against hosts without a playbook.

CommandDescription
ansible all -m pingTest SSH and Python on every host
ansible web -a "uptime"Run a command (default command module)
ansible web -m shell -a "ps aux | grep nginx"Use the shell module for pipes and redirection
ansible web -b -m apt -a "name=nginx state=present"Install a package as root (-b = become)
ansible web -m service -a "name=nginx state=restarted" -bRestart a service
ansible web -m copy -a "src=a.conf dest=/etc/a.conf" -bCopy a file to the hosts
ansible web -m setupGather and print all host facts

Running Playbooks

Apply a playbook with ansible-playbook .

CommandDescription
ansible-playbook site.ymlRun a playbook
ansible-playbook --syntax-check site.ymlValidate YAML and structure only
ansible-playbook --check --diff site.ymlDry run and show would-be changes
ansible-playbook site.yml --limit web1Run against a single host
ansible-playbook site.yml --tags deployRun only tasks with a tag
ansible-playbook site.yml --skip-tags slowSkip tasks with a tag
ansible-playbook site.yml --start-at-task "name"Begin at a named task
ansible-playbook site.yml -e "var=value"Pass an extra variable
ansible-playbook site.yml -KPrompt for the become (sudo) password

Common Modules

Frequently used built-in modules in tasks.

ModuleDescription
ansible.builtin.aptManage packages on Debian-based systems
ansible.builtin.dnfManage packages on RHEL-based systems
ansible.builtin.serviceStart, stop, enable, and restart services
ansible.builtin.copyCopy a file to managed hosts
ansible.builtin.templateRender a Jinja2 template to a file
ansible.builtin.fileSet path state, owner, group, and mode
ansible.builtin.lineinfileEnsure a line is present in a file
ansible.builtin.userCreate and manage user accounts
ansible.builtin.gitCheck out a Git repository
ansible.builtin.systemd_serviceManage systemd units directly

Playbook Keywords

Core directives used inside a play or task.

KeywordDescription
hosts:Target group or host pattern for the play
become: trueRun tasks with privilege escalation (sudo)
vars:Define variables for the play
vars_files:Load variables from external files
tasks:List of tasks to run in order
handlers:Tasks triggered by notify
notify:Trigger a handler when a task changes
when:Run a task only if a condition is true
loop:Repeat a task over a list
register:Save a task result to a variable

Ansible Vault

Encrypt secrets so they are safe to commit.

CommandDescription
ansible-vault create secrets.ymlCreate a new encrypted file
ansible-vault edit secrets.ymlEdit an encrypted file
ansible-vault view secrets.ymlView without editing
ansible-vault encrypt vars.ymlEncrypt an existing plaintext file
ansible-vault decrypt vars.ymlDecrypt a file back to plaintext
ansible-vault rekey secrets.ymlChange the vault password
ansible-playbook site.yml --ask-vault-passPrompt for the vault password at run
ansible-playbook site.yml --vault-password-file .passRead the vault password from a file

Galaxy & Collections

Install and manage roles and collections.

CommandDescription
ansible-galaxy collection install community.generalInstall a collection
ansible-galaxy collection listList installed collections
ansible-galaxy role install geerlingguy.nginxInstall a role from Galaxy
ansible-galaxy role listList installed roles
ansible-galaxy install -r requirements.ymlInstall from a requirements file
ansible-galaxy init my_roleScaffold a new role directory