What is an Ansible playbook and how is it structured?
Learn what an Ansible playbook is, how it is structured with plays, tasks and handlers, why it is idempotent, plus YAML examples and interview questions.
Expected Interview Answer
An Ansible playbook is a YAML file that defines automation as an ordered list of plays, where each play maps a set of hosts to the tasks and roles that should run against them.
A playbook is a YAML list at the top level; each item is a play containing keys like hosts, become, vars, and tasks. Each task calls a module with arguments, optionally naming the task and notifying handlers. Playbooks are declarative and idempotent — running them repeatedly converges hosts to the desired state — and they can pull in reusable roles, variables, templates and handlers to keep automation organized.
- Human-readable YAML describes the desired state
- Idempotent — safe to run repeatedly
- Groups related tasks into plays targeting specific hosts
- Reusable via roles, includes and imports
- Version-controllable infrastructure as code
AI Mentor Explanation
A playbook is like a captain's match plan written down for the whole day: it is an ordered list of plays, each play being a phase such as the powerplay or death overs. Within each phase there are specific tasks — set this field, bowl this line — assigned to named players. The plan is followed top to bottom, and running the same plan on a fresh pitch still produces the same intended field settings and bowling order.
Step-by-Step Explanation
Step 1
Start the YAML document
Begin the file with --- and write the top level as a list of plays.
Step 2
Define a play
Give each play a name, a hosts target, and options like become: true for privilege escalation.
Step 3
Add variables
Declare vars, vars_files, or reference group_vars/host_vars for reusable values.
Step 4
List tasks
Under tasks, add named entries that each call one module with its arguments.
Step 5
Wire handlers
Add handlers triggered by notify so services restart only when a task reports changed.
Step 6
Run and verify
Execute with ansible-playbook site.yml, using --check for a dry run to confirm idempotency.
What Interviewer Expects
- Playbook is YAML and a list of plays
- Knowledge of core play keys: hosts, become, vars, tasks, handlers
- Understanding of idempotency
- How roles and includes structure larger playbooks
- How to run a playbook with ansible-playbook and --check
Common Mistakes
- Confusing a playbook with a single task or a role
- Bad YAML indentation breaking the play structure
- Assuming tasks run in parallel rather than in order per host
- Forgetting handlers only fire on a changed result
- Not using become when a task needs root privileges
Best Answer (HR Friendly)
“An Ansible playbook is a plain-text file that lists, step by step, what should be set up on which servers. It is organized into plays that target groups of machines and tasks that do the actual work, and running it repeatedly always leaves the servers in the same intended state.”
Code Example
---
- name: Configure web servers
hosts: webservers
become: true
vars:
http_port: 80
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
notify: Restart nginx
handlers:
- name: Restart nginx
ansible.builtin.service:
name: nginx
state: restartedFollow-up Questions
- What is the difference between a play and a task?
- How does idempotency work in Ansible playbooks?
- How do roles help organize large playbooks?
- What is the purpose of handlers and the notify keyword?
- How do you do a dry run of a playbook?
MCQ Practice
1. An Ansible playbook is written in which format?
Playbooks are YAML files structured as a list of plays.
2. Which key inside a play maps it to a set of target machines?
The hosts key specifies which inventory hosts or groups the play runs against.
3. Handlers are triggered when?
A handler runs only when a task that notifies it reports a changed state.
Flash Cards
What is an Ansible playbook? — A YAML file defining automation as an ordered list of plays run against inventory hosts.
What is a play? — A mapping of a group of hosts to the tasks, roles and variables that run against them.
What makes playbooks idempotent? — Modules check current state and only make changes needed to reach the desired state.
What is a handler? — A task that runs only when notified by another task reporting a changed result.
How do you run a playbook? — With ansible-playbook playbook.yml, adding --check for a dry run.