How to Write Your First Ansible Playbook Without YAML Headaches
A clear introduction to Ansible playbooks: inventory, plays, tasks, modules, handlers, roles and variables, plus the lint rules that catch common mistakes.

How to Write Your First Ansible Playbook Without YAML Headaches
Ansible lets you describe how servers should be set up in plain files, then apply that description to one machine or a thousand. It needs no agent on the servers, only SSH and Python. The learning curve is not Ansible itself but its YAML: indentation, quoting and the exact names of options. This guide explains the core ideas and how to avoid common mistakes, with the free Ansible playbook builder to assemble playbooks visually.
Inventory: which machines
An inventory lists your hosts and puts them into groups such as web, db or staging. Each host can carry variables, for example ansible_host=203.0.113.10 for its address or ansible_user=deploy for the SSH user. Groups can contain other groups, which lets you target "all web servers in production" precisely.
Plays and tasks: what to do
A playbook is a list of plays. Each play chooses hosts and lists tasks. A task calls one module with parameters:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
Modules are idempotent: they describe the desired state, not a command. Running the task twice installs nginx once, and the second run reports "ok" instead of "changed". This is what makes playbooks safe to rerun.
Prefer modules over shell commands
It is tempting to write shell: apt-get install -y nginx. It works, but it always reports "changed", it does not support check mode, and it hides intent. Use the apt, dnf, service, file, user and git modules where they exist, and keep command and shell for the rare cases with no module. When you do run a command, add creates or changed_when so Ansible knows whether anything changed.
Handlers: react to changes
Restarting a service on every run is wasteful and causes downtime. A handler runs only when a task that notifies it actually changed something:
notify: Reload nginx
The handler named exactly "Reload nginx" then runs once at the end of the play. A typo in either name means the handler never runs, which is one of the most common silent bugs.
Variables and conditions
Variables keep playbooks reusable: a port or a site name defined once and used in several tasks as {{ site_name }}. Conditions use when: with a plain Jinja expression such as ansible_os_family == 'Debian', without curly braces, because when is already evaluated as an expression.
Roles: organise as you grow
When a playbook grows, group related tasks, handlers, templates and default variables into a role, such as nginx or baseline. A role lives in a standard folder layout and can be reused across projects. Plays then list roles instead of long task lists.
YAML traps worth knowing
- File modes must be quoted:
mode: '0644'. Unquoted 644 is read as a decimal number and produces the wrong permissions. - Use
trueandfalse; older yes and no values are discouraged by current lint rules. - Indentation must use spaces, never tabs.
- Use fully qualified module names like ansible.builtin.copy so it is clear which collection provides them.
Test before you apply
Run ansible-playbook site.yml --check --diff first. Check mode reports what would change without changing it, and diff shows file changes line by line. Run ansible-lint as part of your workflow; it catches style and safety issues before they reach a server.
Limits
A playbook can only be as correct as the knowledge behind it: Ansible will happily apply a wrong configuration to every server at once. Start with a staging group, keep secrets in Ansible Vault rather than plain variables, and review playbook changes like code.



