← Back to cheat sheets

Infrastructure as Code

Ansible cheat sheet

Ansible automates configuration and provisioning over SSH.

On this page

Table of contents

Use this page for fast recall. Use Full documentation when you want the complete end-to-end path.

Quick workflow

A simple 5-step flow you can follow when using Ansible in real work.

1) Setup

Install/run the tool and confirm version. Create a minimal config.

2) Small change

Do one small action end-to-end to prove the workflow.

3) Validate

Check output, logs, and status. Catch mistakes early.

4) Automate

Convert it into a repeatable script or pipeline step.

5) Productionize

Add safety: secrets, rollback, observability, and docs.

Workflows you will actually reuse

These are practical sequences you can copy into your own checklist or runbook.

Idempotent server setup

Goal: Make server provisioning repeatable and safe to run multiple times.

- Define an inventory and host groups.

- Start with one play that installs packages and configures a service.

- Run with `--check --diff` first, then real run.

- Refactor into roles and tag your tasks.

- Add CI linting and run against staging before prod.

Key Concepts

- Inventory

- Playbooks

- Roles

Learning path (high-level):

- Inventory design

- Playbook authoring

- Role-based reuse

Quick Start

Ping hosts

Command

ansible all -m ping

Run playbook

Command

ansible-playbook site.yml

Check syntax

Command

ansible-playbook --syntax-check site.yml

Common Commands

Short descriptions and practical intent. Search, filter, copy, and reuse.

Showing 16

Basics
ansible --version

Show Ansible version.

Ad-hoc
ansible all -m ping

Connectivity check via ping module.

Ad-hoc
ansible all -a 'uptime'

Run ad-hoc command on all hosts.

Ad-hoc
ansible web -a 'df -h'

Run command on group `web`.

Inventory
ansible-inventory -i inventory.ini --list

Print parsed inventory.

Inventory
ansible-inventory -i inventory.ini --graph

Show inventory graph.

Playbooks
ansible-playbook site.yml

Run a playbook.

Playbooks
ansible-playbook site.yml --check

Dry run playbook changes.

Playbooks
ansible-playbook site.yml --diff

Show diffs for changed templates/files.

Playbooks
ansible-playbook site.yml --limit web

Run playbook on a group.

Playbooks
ansible-playbook site.yml -t nginx

Run only specific tags.

Playbooks
ansible-playbook site.yml --start-at-task "Install packages"

Resume from a task.

Modules
ansible-doc copy

Show module docs.

Galaxy
ansible-galaxy collection list

List installed collections.

Galaxy
ansible-galaxy collection install community.general

Install a collection.

Roles
ansible-galaxy init role_name

Create a new role structure.

Copyable snippets

Small blocks you can drop into your terminal, config, or runbook.

Run a playbook safely first

bash

ansible-playbook -i inventory.ini site.yml --check --diff
ansible-playbook -i inventory.ini site.yml

Troubleshooting checklist

When things break, follow this order to stay calm and move fast.

- If SSH fails: confirm host, key, user, and `ansible_ssh_common_args` settings.

- If tasks are not idempotent: use proper modules instead of raw shell commands.

- If variables behave oddly: print `-vvv` and confirm inventory precedence.

Pitfalls

The common mistakes that slow people down when using Ansible.

- Copy-pasting commands without understanding inputs/outputs and side effects.

- Not documenting defaults (ports, paths, credentials) and then getting stuck in prod.

- Skipping logs and metrics when troubleshooting; always collect evidence first.

Mini lab (practice)

Do these tasks in order. You will feel the tool instead of just reading about it.

- Install or run the tool locally (or in Docker) and verify it works with a hello-world action.

- Create a minimal config and run the most common command 3 times (with a small change each time).

- Break something on purpose and document how you debugged it in your Notes.

Interview prompts

Use these to test if you truly understand the basics (and can explain them clearly).

- Explain the tool’s role in a real CI/CD pipeline from commit to production.

- Describe the most common failure you’ve seen with this tool and how you fixed it.

- What would you monitor/alert on for this tool in production?

Official Docs

https://docs.ansible.com/