Ansible Curriculum
What Is A Playbook
While ad-hoc commands are useful for quick, one-off diagnostic tasks, managing configurations and complex multi-stage deployments requires a reusable tool.
An Ansible Playbook is a configuration file written in YAML that defines the desired state of your infrastructure. In a playbook, you map groups of hosts to a list of tasks. Each task calls an Ansible module to perform a specific action.
Because playbooks are written in simple YAML, they act as self-documenting code that is easy to read, write, and manage in version control systems like Git.
In this guide, we will explore the structure of playbooks, compare playbooks with shell scripts, and diagram playbook components.
Playbook Structure Diagram
An Ansible playbook is composed of one or more Plays, which contain target host mappings, configuration variables, and list of Tasks:
Playbooks vs. Bash Shell Scripts
Using Ansible playbooks to automate system administration has several advantages over writing custom bash scripts:
- Declarative Approach: Bash scripts require you to write the exact commands to execute. Playbooks allow you to define the desired final state of the system, and Ansible handles the execution details.
- Idempotency: Playbook tasks check the state of the target system before executing, preventing duplicate modifications. Bash scripts require you to write custom validation checks for every command.
- Structured Output: Playbooks output a color-coded summary log showing exactly which tasks were skipped, completed successfully, or failed across all hosts.
Summary
Ansible playbooks allow you to manage complex configurations and multi-stage deployments in reusable, self-documenting YAML files. By mapping target hosts to list of tasks, you can automate your infrastructure consistently. In the next section, we will cover playbook structures.