Ansible Cheatsheet - Automation

To change configs, install dependencies, or maintain services across dozens of machines, Ansible runs over SSH with no agent and uses declarative idempotent modules — an efficient entry into ops automation. This table goes from one-off ad-hoc commands to Playbook structure and common modules; run ansible --syntax-check and --check as a dry run before actually executing.

SysOps·42 commands·Last updated 2026-07-21

Core Commands 6

ansible all -m ping
Test connectivity to all hosts
ansible-playbook site.yml
Run a playbook
ansible-galaxy init myrole
Init a role directory skeleton
ansible-galaxy install geerlingguy.nginx
Install a role from Galaxy
ansible-vault encrypt secrets.yml
Encrypt a secrets file
ansible-doc -l | grep copy
List modules and search usage

Ad-hoc Commands 7

ansible webservers -m shell -a "uptime"
Run a shell command on a group
ansible all -m setup -a "filter=ansible_os_family"
Gather host facts
ansible all -m copy -a "src=file.txt dest=/tmp/"
Copy a file to remote hosts
ansible all -m service -a "name=nginx state=started"
Start a service
ansible webservers -m yum -a "name=nginx state=latest"
Install or upgrade a package
ansible all -m ping --limit web1
Run only on matching hosts
ansible all -m shell -a "df -h" -f 10
Set fork count to parallelize

Common Modules 7

- name: Install nginx\n apt:\n name: nginx\n state: present
apt/yum package module
- name: Copy config\n template:\n src: nginx.conf.j2\n dest: /etc/nginx/nginx.conf
template rendering module
- name: Start service\n service:\n name: nginx\n state: started\n enabled: yes
service management module
- name: Create user\n user:\n name: deploy\n shell: /bin/bash
user management module
- name: Run command\n shell: npm run build\n args:\n chdir: /app
shell command module
- name: Create dir\n file:\n path: /opt/app\n state: directory\n mode: "0755"
file/directory module
- name: Git clone\n git:\n repo: https://github.com/x/y.git\n dest: /srv/app
git checkout module

Playbook Structure 6

- hosts: webservers\n become: yes\n tasks: [...]
Target hosts and enable sudo
pre_tasks: / post_tasks:
Tasks before/after roles
handlers:\n - name: restart nginx\n service: name=nginx state=restarted
Define a handler (triggered by notify)
vars:\n nginx_port: 80
Inline variables in a playbook
- name: notify handler\n template: ...\n notify: restart nginx
Notify a handler after a change
- import_playbook: db.yml
Import another playbook

Variables & Templates 6

vars_files:\n - vars/secrets.yml
Load variables from a file
- command: ...\n when: ansible_os_family == "Debian"
Conditional run with Jinja2
- user: name={{ item }}\n with_items:\n - alice\n - bob
Loop over a list
- command: ...\n register: result
Register task output to a variable
{{ nginx_port }} / {{ ansible_hostname }}
Jinja2 variable syntax
when: result.rc == 0
Condition on the previous task's result

Inventory 5

[webservers]\nweb1 ansible_host=10.0.1.1\nweb2 ansible_host=10.0.1.2
INI-format inventory
[web:children]\nweb1\nweb2
Define group nesting
ansible all -i hosts.yml --list-hosts
List matched hosts with an inventory
ansible all -i ec2.py --list-hosts
Use a dynamic inventory (cloud)
web1 ansible_host=10.0.1.1 ansible_user=deploy ansible_port=2222
Set host connection params

Roles & Best Practices 5

ansible-galaxy init myrole
Generate a standard role layout
roles/myrole/{tasks,handlers,templates,files,defaults,vars}/main.yml
Standard role directory convention
- hosts: web\n roles:\n - nginx\n - { role: db, when: enable_db }
Use roles in a playbook
- import_role:\n name: nginx\n tags: setup
Statically import a role with tags
[defaults]\ninventory = hosts\nremote_user = deploy\nhost_key_checking = False
ansible.cfg global config

Tips

  • Ansible connects over SSH - no agent needed on targets.
  • template renders configs with Jinja2, more flexible than copy.
  • --check (dry-run) previews changes without applying them.
  • Role layout: tasks/main.yml, handlers/main.yml, templates/, files/, defaults/main.yml.
  • Encrypt secrets with ansible-vault via vars_files; never hardcode.
  • group_vars/ and host_vars/ auto-load per group/host - easier to maintain.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us