This is a complete, hands-on Ansible cheat sheet: install, configure, operate, automate and secure it - with copy-paste commands grouped by task. Bookmark it for day-to-day work, incidents and interviews alike.
Ansible configures servers and orchestrates deploys using idempotent 'playbooks' - YAML that describes desired state and runs over SSH with no agent to install on the targets.
Why you reach for it
- Agentless: it only needs SSH and Python on the target
- Idempotent modules converge a host to the desired state repeatedly and safely
- Readable YAML that doubles as executable documentation
Installation
python3 -m pip install --user ansible
ansible --version
ansible all -i inventory.ini -m pingConfiguration reference
A playbook that installs and enables nginx idempotently.
- hosts: web
become: true
tasks:
- name: Install nginx
ansible.builtin.package:
name: nginx
state: present
- name: Ensure nginx is running and enabled
ansible.builtin.service:
name: nginx
state: started
enabled: true
- name: Deploy site config
ansible.builtin.template:
src: site.conf.j2
dest: /etc/nginx/conf.d/site.conf
notify: reload nginx
handlers:
- name: reload nginx
ansible.builtin.service: { name: nginx, state: reloaded }Core commands you use daily
ansible-playbook -i inventory.ini site.yml --check # dry run
ansible-playbook -i inventory.ini site.yml --diff
ansible-vault encrypt group_vars/prod/secrets.ymlAd-hoc commands
ansible all -i inventory.ini -m ping
ansible web -i inventory.ini -m shell -a 'uptime'
ansible db -i inventory.ini -m service -a 'name=postgresql state=restarted' --becomePlaybooks
ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml --tags deploy --limit web
ansible-playbook site.yml --list-tasksVault & roles
ansible-vault encrypt group_vars/prod/secrets.yml
ansible-vault edit group_vars/prod/secrets.yml
ansible-galaxy role install geerlingguy.nginx
ansible-galaxy collection install community.generalUse it in CI/CD
Lint playbooks and run in check mode in CI.
- run: ansible-lint
- run: ansible-playbook -i inventory.ini site.yml --check --diffBest-practice checklist
- Prefer modules over raw shell; add changed_when/creates to shell tasks
- Structure logic into roles and use group_vars/host_vars
- Encrypt secrets with Ansible Vault and keep the key out of the repo
Common pitfalls to avoid
- Non-idempotent shell/command tasks that run every time regardless of state
- Secrets in plaintext instead of Ansible Vault
- Giant single playbooks instead of reusable roles
Security quick wins
- Vault-encrypt all credentials; rotate the vault password regularly
- Use a dedicated least-privilege deploy user and become only where needed
Works well with
- Terraform - Declarative, provider-agnostic infrastructure as code with a planned, reviewable change workflow.
- packer
- puppet
A cheat sheet is muscle memory on paper - keep it close until the commands live in your fingers.
Want senior help putting Ansible to work - or a team bootcamp on it? CodeWizard runs Israel's largest DevOps community and embeds named senior engineers with a transparent hours log. Book a free consult or explore the Academy.