Playbook
Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language.
Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.
Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially.
Playbooks are the building blocks for all the use cases of Ansible.
Playbook Structure
Each playbook is an aggregation of one or more plays in it. Playbooks are structured using Plays. There can be more than one play inside a playbook.
The function of a play is to map a set of instructions defined against a particular host.
YAML is a strict typed language; so, extra care needs to be taken while writing the YAML files.
There are different YAML editors but we will prefer to use a simple editor like notepad++. Just open notepad++ and copy and paste the below yaml and change the language to YAML (Language → YAML).
A YAML starts with --- (3 hyphens)
YAML Tags
name
This tag specifies the name of the Ansible playbook. As in what this playbook will be doing. Any logical name can be given to the playbook.
hosts
This tag specifies the lists of hosts or host group against which we want to run the task. The hosts field/tag is mandatory.
It tells Ansible on which hosts to run the listed tasks. The tasks can be run on the same machine or on a remote machine.
One can run the tasks on multiple machines and hence hosts tag can have a group of hosts’ entry as well.
vars
Vars tag lets you define the variables which you can use in your playbook. Usage is similar to variables in any programming language.
tasks
All playbooks should contain tasks or a list of tasks to be executed. Tasks are a list of actions one needs to perform.
A tasks field contains the name of the task. This works as the help text for the user. It is not mandatory but proves useful in debugging the playbook.
Each task internally links to a piece of code called a module. A module that should be executed, and arguments that are required for the module you want to execute.
Nginx Playbook
Last updated