Playbook Syntax
Playbooks use YAML, a human‑friendly data serialization language. Understanding the syntax is essential for writing effective automation.
YAML Basics
- Indentation: Use spaces, not tabs. Two spaces per level is the convention.
- Key‑value pairs:
key: value(space after colon). - Lists: Start with a hyphen and space
- item. - Dictionaries: Nested key‑value pairs.
Playbook Structure
A playbook is a list of plays. Each play is a dictionary with keys like
name, hosts, tasks, vars, etc.---
- name: First play
hosts: webservers
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Second play
hosts: databases
tasks:
- name: Install postgresql
apt:
name: postgresql
state: presentTask Syntax
Each task has a
name (descriptive) and a module. The module’s arguments can be written on one line or as a nested dictionary.- name: Copy file with inline args
copy: src=/local/file dest=/remote/file
- name: Copy file with nested args
copy:
src: /local/file
dest: /remote/file
owner: rootImportant: Playbooks Start with "---"
While optional, it’s a best practice to start playbooks with
--- to indicate the start of a YAML document.Checking Syntax
Validate a playbook without running it:
ansible-playbook playbook.yml --syntax-checkTwo Minute Drill
- Use spaces, not tabs; two spaces per indent level.
- Lists start with
-, dictionaries are key‑value pairs. - Each play has
hostsandtasks. - Validate syntax with
--syntax-check.
Need more clarification?
Drop us an email at career@quipoinfotech.com
