In this chapter, we will learn about Playbooks on Ansible.
Playbooks are the files where you write the Ansible code. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the main features of Ansible and tell Ansible what to run. They are like a to-do list for Ansible that contains a to-do list.
Playbooks contain the steps that the user wants to run on a particular machine. Playbooks run sequentially. Playbooks are the building blocks for all Ansible use cases.
playbook is an aggregation of one or more plays in it. Playbooks are structured using Plays. There can be more than one play within a playbook.
The function of a work is to map a set of defined instructions against a particular host.
is a strict typed language; therefore, special care must be taken when writing 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 next yaml and change the language to YAML (Language → YAML).
starts with – (3 hyphens)
Create a Playbook
Let’s start by writing a sample YAML file. We will walk through each section written in a yaml file.
– name: install and configure database hosts: testServer convert: yes vars: oracle_db_port_value: 1521 tasks: -name: Install the Oracle database yum: <code to install the database> -name: make sure the installed service is enabled and running service: name: <name of your service>
The above is an example Playbook where we are trying to cover the basic syntax of a playbook. Save the above content to a file as test.yml. A YAML syntax should follow the correct indentation and one should be a little careful when writing the syntax.
The different YAML tags
Now let’s look at the different YAML tags. The different tags are described below −
name This tag specifies the
name of the Ansible playbook. Like what this playbook will do. Any logical name can be given to the playbook.
This label specifies the lists of hosts or host groups on which we want to run the task. The hosts/tag field/tag is required. It tells Ansible on which hosts to run the listed tasks. Tasks can be run on the same machine or on a remote machine. One can run the tasks on multiple machines and therefore the hosts tag can also have a host group entry.
Vars tag allows you to define the variables that you can use in your playbook. The usage is similar to variables in any programming language.
All playbooks must contain tasks or a list of tasks to run. Tasks are a list of actions that one needs to perform. A task field contains the name of the task. This works as help text for the user. It is not required, but it is useful for debugging the playbook. Each task is linked internally to a code snippet called a module. A module to run and the arguments required for the module you want to run.