Operations 17 min read

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.

Raymond Ops
Raymond Ops
Raymond Ops
Master Ansible Playbooks: From Basics to Advanced YAML Techniques
ansible

commands 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 hosts directive.

Play‑level variables can be defined to set the environment for the play.

Example playbook ( first.yml) contains two plays ("play 1" for nginx hosts and "play 2" for apache hosts), each with two debug tasks:

---
- 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"

Running the playbook with ansible-playbook first.yml produces 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 .yaml or .yml. Understanding YAML fundamentals—objects (key/value), arrays (lists), and scalars—is essential.

Objects example: name: junmajinlong Arrays example:

- Shell
- Perl
- Python

Dictionaries (nested objects) example:

person1:
  name: junmajinlong
  age: 18
  gender: male
person2:
  name: xiaofanggao
  age: 19
  gender: female

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 hosts and tasks. Example converting an ad‑hoc copy command to a playbook ( copy.yml):

---
- hosts: nginx
  gather_facts: false
  tasks:
    - copy:
        src: /etc/passwd
        dest: /tmp

Adding name fields 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 args keyword. All styles are functionally equivalent; choose the one that best fits your readability preferences.

4.5 Specifying target hosts

The hosts directive 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 forks setting (default 5). The controller creates up to forks parallel processes; as soon as a host finishes a task, another host can start, ensuring at most forks hosts run concurrently.

Example playbook demonstrating gather_facts and the effect of forks:

---
- name: first play
  hosts: all
  #gather_facts: false
Ansible ad‑hoc vs playbook illustration
Ansible ad‑hoc vs playbook illustration
YAML basic syntax
YAML basic syntax
Ansible execution strategy diagram
Ansible execution strategy diagram
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Operations
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.