Ansible Playbooks

What are playbooks?

Ansible is an orchestration tool. It needs a list of tasks/instructions to perform on the machines listed in the inventory file. You can execute a task with Ansible more than once using a playbook. Playbooks offer repeatable, reusable, simple configuration management. It can be used for configuration management, orchestrating steps of any manual process on multiple machines in synchronous or asynchronous order.

A playbook is written in YAML format. It is composed of one or more 'plays' in an ordered list. And one play contains a set of tasks that runs on a group of machines.

A simple playbook may look like this -

---

- name: Update web servers
  hosts: webservers
  remote_user: root
  tasks:
    - name: Ensure apache is at the latest version
      yum:
        name: httpd
        state: latest

Different YAML Tags

Let us now go through the different tags in a playbook-

name: Logical name of the task which specifies what this playbook will be doing

hosts: This specifies the lists of hosts against which we want to run the task

vars: This allows you to define and use variables in your playbook

tasks: Tasks are a list of actions the playbook will perform

Let's play with a simple playbook

Create a file as playbook1.yml in ~/ansible/playbook directory. And, enter the details as below:

- name: this is our first play.
  hosts: webserver1
  tasks:
    - name: "create a dummy file on websever1"
      command: touch /tmp/ansible_dummy.txt

This playbook contains a task with the name: "create a dummy file on webserver1"

Which it will perform on host: "webserver1" using the command: touch

Our current inventory file final_inventory.yml looks like -

webserver1 ansible_host=192.168.25.15 ansible_ssh_pass=password ansible_connection=ssh ansible_port=22 ansible_user=root
sqlserver1 ansible_host=192.168.25.16 ansible_ssh_pass=password ansible_connection=ssh ansible_port=22 ansible_user=root

[webservers]
webserver1

[databaseservers]
sqlserver1

[web_database_servers]
webserver1
sqlserver1

Running a playbook

To run your playbook, use the ansible-playbook command:

$ansible-playbook playbook1.yml -i final_inventory.yml

Output- ansible-inventory-file

We will be ignoring the warning regarding 'state=touch' as of now.

Verifying the same on webserver1. Below is the snippet from webserver1 before and after running the command- ansible-playbook-output-sample

Running playbook on a group of hosts

You can run the command on different groups using playbook:

Create a file as playbook2.yml in ~/ansible/playbook directory. And, enter the details as below:

- name: "this is our first play."
  hosts: web_database_servers
  tasks:
    - name: "create a dummy file in web and database server"
      command: touch /tmp/ansible_dummy_webservers.txt
$ansible-playbook playbook2.yml -i final_inventory.yml

Command output: ansible-plybook-output Verifying file creation on webserver1: ansible-plybook-output

Verifying file creation on another server: ansible-plybook-output

Running playbook with multiple tasks & multiple plays

Consider below playbook playbook.yml with multiple plays:

- name: "this is our first play."
  hosts: webserver1
  tasks:
    - name: "creating dummy file on webserver1"
      command: touch /tmp/file_on_webserver1
    - name: "copy hosts files in tmp folder"
      command: cp /etc/hosts /tmp/myhosts
- name: "this is our second play."
  hosts: web_database_servers
  tasks:
    - name: "creating directory in tmp direcotry"
      command: mkdir /tmp/mySecondPlayDir
    - name: "create a dummy file in database and webserver."
      command: touch /tmp/mySecondPlayDir/secondPlay.txt

The playbook is one of the most important elements in Ansible. It is designed to be human-readable and it developed in a basic text language so that everyone can understand it.