Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026Batch 01 · Aarambh — AWS + Agentic AI starts 28 June 2026
All cheatsheets
Linux · Cheatsheet

Ansible Cheatsheet

Inventory, ad-hoc, playbooks, roles, vault, check mode, and the magic vars (hostvars, groups) every playbook author needs.

Updated 2026-05-21 8 min

Inventory & ad-hoc

ansible-inventory -i hosts.ini --listRender inventory as JSON
ansible-inventory --graphTree view of groups + hosts
ansible all -m pingConnectivity test (uses python on target)
ansible web -m setupGather all facts from group 'web'
ansible db -m shell -a 'systemctl status postgresql'Run a shell command
ansible web -m copy -a 'src=./nginx.conf dest=/etc/nginx/nginx.conf' --becomeCopy + sudo

Playbook basics

ansible-playbook site.yml -i hosts.iniRun a playbook
ansible-playbook site.yml --check --diffDry-run + show diffs (best practice)
ansible-playbook site.yml --limit web1Limit to one host
ansible-playbook site.yml --tags 'deploy,restart'Run only tagged tasks
ansible-playbook site.yml --skip-tags 'slow'Skip tagged tasks
ansible-playbook site.yml --start-at-task 'Install nginx'Resume from a task
ansible-playbook site.yml -vvvVerbose (4 levels: v, vv, vvv, vvvv)

Variables & facts

ansible-playbook site.yml -e 'env=prod version=1.2'Extra vars (highest precedence)
ansible-playbook site.yml -e @prod.jsonExtra vars from file
{{ ansible_hostname }} / {{ ansible_default_ipv4.address }}Common facts
{{ hostvars['web1'].ansible_distribution }}Access another host's facts
{{ groups['db'] }}List of hosts in group 'db'
{{ lookup('env', 'HOME') }}Read env var on controller

Roles & collections

ansible-galaxy init roles/nginxScaffold a role
ansible-galaxy collection install community.kubernetesInstall collection
ansible-galaxy install -r requirements.ymlInstall role/collection deps
roles/<name>/{tasks,handlers,defaults,vars,files,templates,meta}Standard role layout

Vault (secrets)

ansible-vault create secrets.ymlCreate encrypted file
ansible-vault edit secrets.ymlEdit
ansible-vault view secrets.ymlView
ansible-vault encrypt_string 'mysecret' --name 'db_pass'Inline-encrypted value
ansible-playbook site.yml --ask-vault-passPrompt for password
ansible-playbook site.yml --vault-password-file ~/.vault_passRead from file

Common task patterns

become: true become_user: postgresPrivilege escalation
when: ansible_os_family == 'Debian'Conditional execution
loop: "{{ packages }}"Iterate over a list
register: result / debug: var=resultCapture + print task output
notify: 'restart nginx'Trigger handler on change
block / rescue / alwaysTry/catch for tasks
delegate_to: localhostRun task on controller, not target

Useful modules

ansible.builtin.apt / yum / dnfPackage management
ansible.builtin.service / systemdManage services
ansible.builtin.templateJinja2 template → file
ansible.builtin.lineinfile / blockinfileIdempotent file edits
ansible.builtin.cronManage crontab entries
community.general.timezoneSet system timezone
kubernetes.core.k8sApply Kubernetes manifests

Performance

forks = 50 (ansible.cfg)Parallelism — default is 5
strategy: freeHosts proceed independently (vs default linear)
pipelining = TrueFewer SSH calls per task (2-4x faster)
fact_caching = jsonfileCache facts across runs

Want the full hands-on training behind this?

Cloudadhar batches walk you through every command in a real production setup — with labs, code reviews, and 1:1 doubt sessions.