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.
Core Commands 6
ansible all -m pingTest connectivity to all hosts
ansible-playbook site.ymlRun a playbook
ansible-galaxy init myroleInit a role directory skeleton
ansible-galaxy install geerlingguy.nginxInstall a role from Galaxy
ansible-vault encrypt secrets.ymlEncrypt a secrets file
ansible-doc -l | grep copyList 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 web1Run only on matching hosts
ansible all -m shell -a "df -h" -f 10Set fork count to parallelize
Common Modules 7
- name: Install nginx\n apt:\n name: nginx\n state: presentapt/yum package module
- name: Copy config\n template:\n src: nginx.conf.j2\n dest: /etc/nginx/nginx.conftemplate rendering module
- name: Start service\n service:\n name: nginx\n state: started\n enabled: yesservice management module
- name: Create user\n user:\n name: deploy\n shell: /bin/bashuser management module
- name: Run command\n shell: npm run build\n args:\n chdir: /appshell 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/appgit 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=restartedDefine a handler (triggered by notify)
vars:\n nginx_port: 80Inline variables in a playbook
- name: notify handler\n template: ...\n notify: restart nginxNotify a handler after a change
- import_playbook: db.ymlImport another playbook
Variables & Templates 6
vars_files:\n - vars/secrets.ymlLoad variables from a file
- command: ...\n when: ansible_os_family == "Debian"Conditional run with Jinja2
- user: name={{ item }}\n with_items:\n - alice\n - bobLoop over a list
- command: ...\n register: resultRegister task output to a variable
{{ nginx_port }} / {{ ansible_hostname }}Jinja2 variable syntax
when: result.rc == 0Condition on the previous task's result
Inventory 5
[webservers]\nweb1 ansible_host=10.0.1.1\nweb2 ansible_host=10.0.1.2INI-format inventory
[web:children]\nweb1\nweb2Define group nesting
ansible all -i hosts.yml --list-hostsList matched hosts with an inventory
ansible all -i ec2.py --list-hostsUse a dynamic inventory (cloud)
web1 ansible_host=10.0.1.1 ansible_user=deploy ansible_port=2222Set host connection params
Roles & Best Practices 5
ansible-galaxy init myroleGenerate a standard role layout
roles/myrole/{tasks,handlers,templates,files,defaults,vars}/main.ymlStandard 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: setupStatically import a role with tags
[defaults]\ninventory = hosts\nremote_user = deploy\nhost_key_checking = Falseansible.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