Stage 4 · Provision
Playbooks & Variables
Playbook Structure
Plays, tasks, hosts, and the YAML you will write daily.
YAML Basics for Ansible
Ansible playbooks are YAML files. YAML uses indentation (2 spaces) to define structure. Lists use hyphens, mappings use colons. Ansible is strict about indentation — inconsistent spacing causes parse errors.
# Mapping (key: value)
name: nginx
# List (hyphen items)
packages:
- nginx
- python3
- git
# Nested structure
server:
host: example.com
port: 443Always use 2-space indentation. Never use tabs. Keep lines under 160 characters for readability.
Play Structure
A playbook consists of one or more plays. Each play maps a group of hosts to a list of tasks. The hosts key specifies which inventory group the play targets.
---
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install nginx
package:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: startedThe name field is optional but strongly recommended. It makes playbook output readable and helps with debugging.
Task Anatomy
Each task executes a module with specific parameters. Tasks run sequentially by default. If a task fails, subsequent tasks are skipped unless you handle the error.
- name: Create application user
user:
name: appuser
shell: /bin/bash
home: /home/appuser
system: yes
state: present
become: yes
register: user_result
tags: [users, setup]register captures the task result in a variable. tags let you run specific subsets of tasks with --tags.
Multiple Plays
A playbook can contain multiple plays targeting different host groups. This lets you configure different tiers — databases, web servers, load balancers — in one file.
---
- name: Configure databases
hosts: dbservers
become: yes
tasks:
- name: Install PostgreSQL
package:
name: postgresql
state: present
- name: Configure web servers
hosts: webservers
become: yes
tasks:
- name: Install nginx
package:
name: nginx
state: present
- name: Configure app
template:
src: templates/app.conf.j2
dest: /etc/app/config.yml
notify: Restart app
handlers:
- name: Restart app
service:
name: app
state: restartedPlays execute in order. Database servers are configured before web servers. Handlers run at the end of each play.
Keep playbooks focused. A web server configuration playbook should not also configure databases. Compose smaller playbooks into larger workflows using import_playbook or include_tasks.
Playbook Best Practices
- Always use name fields on every play and task
- Use YAML inventory for complex host variables
- Keep tasks declarative — prefer modules over shell
- Use handlers for service restarts and conditional actions
- Tag tasks for selective execution
- Start with --check mode before production runs
Inconsistent indentation is the most common playbook error. Use an editor with YAML linting to catch these before running playbooks. Two spaces, no tabs.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.