What are playbooks?

Ansible playbooks are high-level automation scripts that are written in YAML to define the series of actions that we want to perform on a managed node. A playbook is structured and can include the following components:

  • Play: A play is the highest-level structure in a playbook. It defines a set of tasks to be executed on a specific group of target hosts. Each playbook can contain one or more plays.
  • Hosts: In each play, we specify the hosts or groups of hosts on which the tasks should be executed. We can define hosts in our playbook or reference them from an inventory file.
  • Tasks: Tasks are the individual actions to be performed on the target hosts. They are executed sequentially within a play.
  • Handlers: Handlers are special tasks that are triggered by specific events. For example, we can define a handler to restart a service when a configuration file is modified. Handlers are not executed unless explicitly called by a task.
  • Ansible modules: Modules are the small, reusable units of code that are referenced within our tasks.

Here is a generic structure of a playbook containing only one play:

Get hands-on with 1200+ tech skills courses.