Skip to content

Playbook Syntax

Ansible playbooks are structured YAML files that define a series of tasks to configure and manage systems. At their core, playbooks organize actions into plays, which target specific hosts or groups, and break down complex operations into tasks, handlers, and variables. Understanding their syntax and structure is essential for writing effective automation workflows.


Plays: The Building Blocks of a Playbook

A playbook consists of one or more plays, each targeting a set of hosts. Plays define the actions to execute on those hosts.

Basic Play Structure

- name: Ensure Apache is installed and running
  hosts: webservers
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
  handlers:
    - name: Restart Apache
      service:
        name: apache2
        state: restarted
  • name: A human-readable description of the play.
  • hosts: Specifies the target hosts (e.g., webservers from an inventory).
  • tasks: A list of tasks to execute.
  • handlers: Special tasks that run only when notified by other tasks.

Tasks: Atomic Units of Work

Each task represents a single action, such as installing software or configuring a service. Tasks use modules to perform operations.

Task Syntax

- name: Ensure Nginx is installed
  apt:
    name: nginx
    state: present
  • name: Describes the task.
  • module: The Ansible module to use (e.g., apt, service).
  • arguments: Parameters passed to the module.

Tasks are executed in the order they appear. Use when conditions to control task execution:

- name: Restart service only if config changed
  service:
    name: httpd
    state: restarted
  when: config_changed is true


Handlers: Efficient Service Management

Handlers are tasks that run only when notified by another task. They are ideal for restarting services or applying changes that require a reload.

Handler Syntax

handlers:
  - name: Restart Apache
    service:
      name: apache2
      state: restarted
  • Use notify in tasks to trigger handlers:
    - name: Update configuration file
      copy:
        src: httpd.conf
        dest: /etc/httpd/
      notify: Restart Apache
    

Handlers are executed once per playbook, even if multiple tasks notify them. This avoids unnecessary repeated operations.


Variables: Reusable Configuration

Variables store values for reuse across tasks, plays, or the entire playbook. They can be defined in the playbook, inventory, or environment.

Variable Syntax

vars:
  http_port: 80
  db_password: "secure_password"
  • Use variables in tasks:

    - name: Configure web server
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      vars:
        http_port: "{{ http_port }}"
    

  • Access variables via {{ variable_name }}.


Key Takeaways

  • Plays organize tasks by target hosts and define the overall workflow.
  • Tasks are atomic operations using modules, with optional conditions (when).
  • Handlers optimize resource management by running only when notified.
  • Variables enable reuse and dynamic configuration across the playbook.
  • YAML syntax requires consistent indentation and proper structure to avoid errors.