Master Ansible Playbooks: From Basics to Advanced YAML Techniques
This article explains the limitations of ad‑hoc Ansible commands, introduces the concepts of playbooks, plays, and tasks, demonstrates YAML syntax with examples, shows how to write and run playbooks, and details host selection patterns and execution strategies for efficient automation.
ansiblecommands can only execute a single task at a time (Ad‑hoc mode), which is weaker than plain SSH execution. The real power of Ansible lies in playbooks , which act as a solid lever for automation.
4.1 Relationship between playbook, play, and task
Think of a playbook as a script for a movie: each play corresponds to a scene, and each task corresponds to a shot within that scene. A playbook can contain multiple plays, each with multiple tasks, enabling orchestration of complex workflows.
Key points:
One playbook can define one or more plays.
Each play can define one or more tasks.
Special task types:
pre_tasks(run before normal tasks) and
post_tasks(run after normal tasks).
Each play must specify target hosts with the
hostsdirective.
Play‑level variables can be defined to set the environment for the play.
Example playbook (
first.yml) contains two plays ("play 1" for
nginxhosts and "play 2" for
apachehosts), each with two debug tasks:
<code>---
- name: play1
hosts: nginx
gather_facts: false
tasks:
- name: task1 in play1
debug:
msg: "output task1 in play1"
- name: task2 in play1
debug:
msg: "output task2 in play1"
- name: play2
hosts: apache
gather_facts: false
tasks:
- name: task1 in play2
debug:
msg: "output task1 in play2"
- name: task2 in play2
debug:
msg: "output task2 in play2"
</code>Running the playbook with
ansible-playbook first.ymlproduces detailed output showing each play, each task, and the result per host, followed by a recap summarizing successes and changes.
4.2 Playbook syntax: YAML
Ansible playbooks use YAML, a concise format that simplifies JSON. Files typically end with
.yamlor
.yml. Understanding YAML fundamentals—objects (key/value), arrays (lists), and scalars—is essential.
Objects example:
<code>name: junmajinlong</code>Arrays example:
<code>- Shell
- Perl
- Python</code>Dictionaries (nested objects) example:
<code>person1:
name: junmajinlong
age: 18
gender: male
person2:
name: xiaofanggao
age: 19
gender: female</code>Composite structures, string continuation, null values, and quoting rules are also covered, with examples illustrating each case.
4.3 Writing a playbook
After learning YAML, you can write a playbook. A playbook must contain
hostsand
tasks. Example converting an ad‑hoc copy command to a playbook (
copy.yml):
<code>---
- hosts: nginx
gather_facts: false
tasks:
- copy:
src: /etc/passwd
dest: /tmp
</code>Adding
namefields to plays and tasks improves readability.
4.4 Passing module parameters
Parameters can be passed as a single string, as separate lines, or using the
argskeyword. All styles are functionally equivalent; choose the one that best fits your readability preferences.
4.5 Specifying target hosts
The
hostsdirective supports various patterns: direct host names, group names,
all, indexed hosts (e.g.,
nginx[1]), ranges, wildcards, and regular expressions prefixed with
~. Intersection (
&) and exclusion (
!) operators allow fine‑grained host selection.
4.6 Default task execution strategy
Ansible executes tasks across hosts in batches determined by the
forkssetting (default 5). The controller creates up to
forksparallel processes; as soon as a host finishes a task, another host can start, ensuring at most
forkshosts run concurrently.
Example playbook demonstrating
gather_factsand the effect of
forks:
<code>---
- name: first play
hosts: all
#gather_facts: false
</code>Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.