Ansible Cheat Sheet

Basic Commands

CommandDescription
ansible <hostname> -m pingTest connectivity to a host
ansible-playbook <playbook>.ymlRun an Ansible playbook
ansible-vault create <filename>.ymlCreate a new encrypted file
ansible-vault edit <filename>.ymlEdit an encrypted file
ansible-vault view <filename>.ymlView an encrypted file

Inventory Management

CommandDescription
ansible-inventory --listDisplay the inventory
ansible-inventory --graphDisplay the inventory as a graph
ansible-inventory --host <hostname>Display information for a specific host
ansible-inventory --yaml --host <hostname>Display the YAML for a specific host

Playbooks

CommandDescription
hosts: <hostname>Specify the target host(s) for the playbook
gather_facts: trueGather facts about the target host(s)
tasks:Define a list of tasks to be executed
when: <condition>Execute a task conditionally
vars:Define variables for use in the playbook
roles:Include a role in the playbook
include_vars:Include variables from an external file

Modules

ModuleDescription
fileManage files and directories
aptManage packages on Ubuntu/Debian systems
yumManage packages on CentOS/Red Hat systems
serviceManage system services
userManage system users and groups
copyCopy files from the control node to the target hosts
templateCopy template files from the control node to the target hosts and substitute variables
lineinfileManage lines in text files
commandRun a command on the target hosts
shellRun a shell command on the target hosts
cronManage cron jobs on the target hosts
iptablesManage firewall rules on the target hosts
wait_forWait for a specific condition to be met
debugDisplay debug information

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.