• Learn data science, cloud computing, machine learning, and many other specialization courses.

Learn about Ansible Playbook to automate the tasks.

Previously, we discussed Ansible introduction, installation guide, and next, let’s talk about Playbook and its building blocks.

What is Ansible Playbook?

The playbook is Ansible automation language. It is a simple file with a set of instructions. It is in playbooks where we define what we want Ansible to do. Playbooks contain plays which run tasks, and these tasks run modules. Their tasks execute sequentially.

For example, it can be as simple as running a series of command on different servers in a sequence and restarting those servers in order. Or it could be as complex as deploying and provisioning hundreds of VMs in a public and private cloud including their load balancing, monitoring, network setups.

Playbook Language

Playbooks are simple files written in YAML code.

YAML is a data serialization language. It is human and machine-readable. You don’t require any special coding skill to write YAML codes. You can think of data serialization language as a translator for breaking down all your data structure and serialize them in an order which can be reconstructed again for later use. You can use this reconstructed data structure in the same environment or even in a different environment.

Below is a sample YAML file to install mysql:

---
- name: Install MySQL
  hosts: geekflare-mysql-service
  tasks:
    - name: Install MySQL
      action: $ansible_pkg_mgr pkg=mysql-server state=installed
    - name: Add Python MySQL DB
      action: $ansible_pkg_mgr pkg=python-mysqldb state=installed

Ansible Playbook Basics

This part of the article covers the basic Ansible concepts to understand more about Ansible Playbook.

Hosts and Users

Ansible needs target machines in the infrastructure on which plays must be deployed from Ansible playbook. Hosts are added to Ansible inventory through their IP addresses. Hosts are a list of one or more groups or host patterns separated by a colon. The remote_user contains the name of the user account.

---
- hosts: ProdServers
  remote_user: geekflare

Variables

Variables enable you to alter how a playbook runs.  They can be used nearly everywhere in the playbook and can be inherited from an inventory, explicitly set at runtime, discovered at the start of a playbook run. You can define a variable name using letters, numbers, and underscores, but it must start only with a letter.

For example, port_01 is a valid variable, whereas 01_post is an invalid variable. Here is an example of variables in an Ansible Playbook:

vars:
    http_port: 80
    max_clients: 200

Inventories

To run a playbook, you need a list of targets on which to you want automation to happen. This is what an inventory does. Inventory lists can be built and stored in several different ways, including static files, i.e., Ansible Hosts. Or it can be dynamically generated via an inventory script that will pull a list of hosts for an external source.

You can also specify a variable as part of an inventory list. Inventories are ultimately a list of things you want to automate across.

[webservers]
192.168.20.1
192.168.20.2
192.168.20.4
[dbservers]
172.17.1.56
172.17.1.57

Tasks

Plays in ansible playbook run tasks. The task is responsible for running ansible modules. At a time, only one task can run, and tasks get executed in sequential order. Their tasks are written in YAML, which is pretty much English like language.

For examples: install package_name, Update software_name, etc. Below is an example of a task in Ansible playbook to install httpd:

tasks:
  - name: Install httpd Package
    yum: name=httpd state=latest

Handlers

Handlers are a special kind of tasks.

They can be triggered by a task and are run once at the end of the play. It is used to send notifications if there is any change in the configuration file, for example, notify service to start apache once it gets installed. “notify” syntax is used to call handlers. Below is an example of a handler to restart apache:

---
- hosts: all
  tasks:
  - name: ensure apache is at the latest version
    yum: name=httpd state=latest
    notify:
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

Create and Run Your First Ansible Playbook

Let me tell you how to write a playbook. Every playbook starts with three dashes (—) on the top.

The first thing you mention in an Ansible Playbook is the host machines on which you want to run the playbook.

Then you can mention variables by gathering facts; then you can mention the different tasks that you want to perform. Now, remember that the task gets executed in the same order that you write them. For example, you want to install software A first and then software B, make sure that the first task written in the playbook would be to install software A and the next task could install software B.

Then you have got handlers at the bottom. The handlers are also tasks, but the difference is to execute handlers you need some sort of triggers in the list of tasks to run handlers.

Let me show you how to create an ansible playbook to install and start nginx on a host and start it.

Create a .yml file where you will put your YAML codes for creating an ansible playbook.

gedit nginx.yml

Put the below YAML code in this file and save the file.

---
- hosts: Client
  sudo: yes
  vars:
    - server_port: 8080

  tasks:
    - name: Installs nginx web server
      apt: pkg=nginx state=installed update_cache=true
      notify:
        - start nginx

  handlers:
    - name: start nginx
      service: name=nginx state=started

The above YAML file starts with hosts, I want to run this playbook on the client machine (Client). The client IP address is already saved in /etc/ansible/hosts file.

The next line allows running the tasks with sudo privileges.

Then I have defined server_port as a variable in this playbook.

Then comes my first task in this ansible playbook to install an Nginx web server. This task also has notify parameter, which means after this task a handler with run.

Finally, I have written a handler to start nginx on the client machine.

Now that you are clear with the YAML codes in the ansible playbook let’s run the playbook. Below is the syntax to run an ansible playbook:

ansible-playbook file_name.yml

Now, I will run the ansible playbook, which I create to install and start nginx.

[email protected]:/home/geekflare# ansible-playbook nginx.yml 

PLAY [Client] ******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [node1]

TASK [Installs nginx web server] ***********************************************

changed: [node1]

RUNNING HANDLER [start nginx] **************************************************
ok: [node1]

PLAY RECAP *********************************************************************
node1                      : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run the command below to check if the nginx server was installed and started correctly by the ansible playbook.

[email protected]:/home/geekflare# ps waux | grep nginx
root      3021  0.0  0.0  77676  1516 ?        Ss   15:27   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

Conclusion

That was about Ansible playbook. I hope this article helped you to learn how you can create and run an Ansible playbook. Check out this Course, if you want to learn advanced topics in Ansible, including Roles, Jinja2, Lookups, Filter, Custom Modules.