# ansible / foundations / yaml

Write Playbooks in YAML

YAML is a human-readable data serialization language. Combined with Ansible Playbooks, it provides a powerful, declarative way to orchestrate tasks, authorize system arrangements, and manage infrastructure efficiently.

$ ansible-playbook web.yml --syntax-check
Outputplaybook: web.yml
StatusSyntax is valid
FormatDictionary & Lists
Spaces
Indentation
Key/Val
Structure
# yaml

What is YAML?

YAML stands for "Yet Another Markup Language" (or recursively, "YAML Ain't Markup Language"). It is widely supported across programming languages because it represents structured data in a way that is incredibly easy to read and maintain.

structure

Indentation Driven

Structured data is represented purely by indentation. This keeps files clean and eliminates the need for curly braces {} or tags found in JSON or XML.

data types

Lists and Dictionaries

Every item in an Ansible playbook is fundamentally a list of key/value pairs, commonly referred to as a "hash" or a "dictionary".

# orchestration

What is a Playbook?

automation

The Blueprint

A playbook is basically a plan for defining and orchestrating tasks to be executed on managed nodes. It allows you to automate repetitive tasks and manage your entire infrastructure as code.

Playbook Structure

  • ---

    Every valid YAML file must begin with three dashes.

  • hosts

    Defines which machines (from your inventory) the tasks should target.

  • tasks

    A sequential list of actions (using modules) to execute against the target hosts.

# example

A Real Webserver Playbook

web.ymlYAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
---
- name: Configure Webserver
  hosts: node2
  become: yes
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present

    - name: enable apache
      service:
        name: httpd
        enabled: yes
        state: started

    - name: content
      copy:
        content: "welcome to webserver"
        dest: /var/www/html/index.html
        mode: "0644"

    - name: open firewall port
      firewalld:
        service: httpd
        permanent: yes
        state: enabled

Run Order

  • install

    Ensures the Apache (httpd) package is downloaded and present on node2.

    yum module
  • enable

    Starts the web service immediately and ensures it will turn on automatically upon reboot.

    service module
  • seed

    Injects raw HTML content straight into the index.html file with standard web permissions (0644).

    copy module
  • network

    Opens the native firewall to allow inbound HTTP traffic permanently.

    firewalld module
# best-practices

Strict YAML Rules

CRITICAL RULE

Never use the Tab key for indentation. YAML strictly forbids tabs. You must use the spacebar to represent indentation (usually 2 spaces per nested level). Tabs will cause your playbook execution to fail instantly.

RELIABILITY

Playbooks are completely idempotent. If you run the webserver playbook above a second time, Ansible will see that Apache is already installed and the firewall is already open, and it will safely do nothing.