🎉 DevOps Interview Prep Bundle is live — 1000+ Q&A across 20 topicsGet it →
All Cheatsheets

Ansible Cheatsheet

Ansible commands, playbook syntax, inventory, variables, loops, conditionals, roles, and vault for configuration management.

7 sections53 commandsClick any row to copy

Ad-Hoc Commands

ansible all -m ping -i inventory.ini
ansible webservers -m command -a 'uptime'
ansible all -m shell -a 'df -h' -i inventory.ini
ansible db -m apt -a 'name=postgresql state=present' --become
ansible all -m copy -a 'src=file.conf dest=/etc/app/file.conf' --become
ansible all -m service -a 'name=nginx state=restarted' --become
ansible all -m setup -a 'filter=ansible_distribution'
ansible all -m gather_facts --tree /tmp/facts/

Ping all hosts to test connectivity

Run command on webservers group

Shell command (supports pipes/redirects)

Install package with sudo

Copy file to all hosts

Restart service on all hosts

Gather specific facts from hosts

Save facts to files for inspection

Inventory

[webservers] web1 ansible_host=10.0.0.1 web2 ansible_host=10.0.0.2 ansible_port=2222
[all:vars] ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/key.pem
ansible-inventory -i inventory.ini --list
ansible-inventory -i inventory.ini --graph
ansible all -i 'server1,server2,' -m ping
ansible all -i ./inventory/ -m ping
ansible all -i aws_ec2.yml -m ping

INI inventory with custom host and port

Variables for all hosts in inventory

Show parsed inventory as JSON

Show inventory host tree

Inline inventory (comma after last host)

Use directory as dynamic inventory

Use AWS dynamic inventory plugin

Playbook Basics

ansible-playbook site.yml -i inventory.ini
ansible-playbook site.yml --check
ansible-playbook site.yml --diff
ansible-playbook site.yml --check --diff
ansible-playbook site.yml --tags deploy
ansible-playbook site.yml --skip-tags test
ansible-playbook site.yml --limit webservers
ansible-playbook site.yml --limit 'web1,web2'
ansible-playbook site.yml -e 'version=1.2.3'
ansible-playbook site.yml --start-at-task 'Deploy app'

Run a playbook

Dry run — show changes without applying

Show file diffs when files change

Dry run with diffs (most useful preview)

Run only tasks tagged 'deploy'

Skip tasks tagged 'test'

Run only on webservers group

Run only on specific hosts

Pass extra variable on command line

Start from a specific task name

Playbook YAML Syntax

- name: Install nginx apt: name: nginx state: present update_cache: yes become: true
- name: Copy config template: src: nginx.conf.j2 dest: /etc/nginx/nginx.conf owner: root mode: '0644' notify: Restart nginx
handlers: - name: Restart nginx service: name: nginx state: restarted
- name: Wait for port 8080 wait_for: port: 8080 host: localhost timeout: 30
- name: Get git commit command: git rev-parse HEAD register: git_hash - debug: msg='{{ git_hash.stdout }}'
when: ansible_distribution == 'Ubuntu'
when: env == 'production'

Install package task with sudo

Deploy template and trigger handler

Handler — runs only when notified

Wait for service to be available

Run command and use output

Conditional — run only on Ubuntu

Conditional based on variable

Loops & Variables

loop: - nginx - postgresql - redis
loop: '{{ packages }}'
loop: '{{ users | dict2items }}'
vars: app_port: 8080 app_name: myapp
vars_files: - vars/common.yml - vars/{{ env }}.yml
set_fact: deploy_timestamp: '{{ ansible_date_time.iso8601 }}'
{{ hostvars['web1']['ansible_host'] }}

Loop over a list

Loop over a variable list

Loop over dictionary items

Define variables in play

Load variables from files (including dynamic)

Set a fact (variable) during play

Access variable from another host

Ansible Vault

ansible-vault create secrets.yml
ansible-vault edit secrets.yml
ansible-vault encrypt existing.yml
ansible-vault decrypt secrets.yml
ansible-vault view secrets.yml
ansible-playbook site.yml --ask-vault-pass
ansible-playbook site.yml --vault-password-file ~/.vault_pass
ansible-vault encrypt_string 'mypassword' --name 'db_password'

Create new encrypted file

Edit encrypted file

Encrypt an existing file

Decrypt a file (shows plaintext)

View encrypted file without decrypting

Run playbook, prompt for vault password

Run playbook with vault password file

Encrypt a single string value

Roles & Galaxy

ansible-galaxy role init my_role
ansible-galaxy install geerlingguy.nginx
ansible-galaxy install -r requirements.yml
ansible-galaxy collection install community.kubernetes
ansible-galaxy list
roles: - geerlingguy.nginx - my_role

Create role directory structure

Install role from Ansible Galaxy

Install roles from requirements file

Install a collection

List installed roles and collections

Use roles in a playbook