> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/module-13-ansible/playbook.md).

# Playbook

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language.&#x20;

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.&#x20;

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.&#x20;

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
--- 
   name: install and configure DB
   hosts: testServer
   become: yes

   vars: 
      oracle_db_port_value : 1521
   
   tasks:
   -name: Install the Oracle DB
      yum: <code to install the DB>
    
   -name: Ensure the installed service is enabled and running
   service:
      name: <your service name>
```

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.&#x20;

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.&#x20;

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.&#x20;

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.&#x20;

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

```yaml
---
- name: Install and configure NGINX
  hosts: webservers
  become: yes
  tasks:
    - name: Update APT package index
      apt:
        update_cache: yes

    - name: Install NGINX
      apt:
        name: nginx
        state: present

    - name: Ensure NGINX is running
      service:
        name: nginx
        state: started
        enabled: yes
```
