Level 1
0 / 100 XP

Playbooks and Plays Overview

In this lesson, we'll explore the concepts of playbooks and plays in Ansible, which are fundamental to understanding how Ansible automates tasks across multiple systems. By the end of this lesson, you will have a clear understanding of what playbooks and plays are, how they function, and why they are vital in the Ansible ecosystem.

What are Ansible Playbooks?

Ansible playbooks are the core files where Ansible code is written. Written in YAML, playbooks are a series of instructions that tell Ansible what to execute. They are essentially the automation scripts that Ansible uses to configure, deploy, and manage nodes (servers). Each playbook can contain multiple plays.

Structure of a Playbook

A playbook is composed of one or more "plays". These plays are sets of tasks that are executed on a particular set of hosts to achieve your desired state on those hosts.

Here is a high-level view of a playbook's structure:

  • Playbook: The entire YAML file containing all the plays.
  • Play: A set of tasks executed on a group of hosts.
  • Tasks: The operations (or actions) performed by Ansible, usually through Ansible modules.

Ansible Playbook, Plays, and Tasks breakdownAnsible Playbook, Plays, and Tasks breakdown

Understanding Plays

Plays are the heart of a playbook. Each play is designed to target a specific group of hosts and achieve a specific goal on these hosts. For example, a play might be used to install a particular software package on all web servers or to restart services on all database servers.

The power of Ansible comes from its simplicity and flexibility. A single playbook can contain multiple plays, each targeting different sets of hosts with different tasks. This allows for complex orchestration and coordination across different servers and groups within your infrastructure.

Tasks in Plays…