Playbook In Ansible

What is Ansible?

Ansible is an open-source automation platform. It is a simple automation language that can perfectly describe an IT application infrastructure in Ansible Playbooks. It is also an automation engine that runs Ansible Playbooks.

Ansible is developed in Python, it is lightweight and has fast deployment. It can also be run from CLI commands without using configuration files such as reboot, whether the server is running or not. For relatively complex tasks, its configuration is handled by the YAML syntax in a configuration file called a playbook.

Applications of Ansible

Configuration Management

Application Deployment

Provisioning

Orchestration

Security

Terms In Ansible

Control Nodes

Ansible is simple to install. The Ansible software only needs to be installed on the control node (or nodes) from which Ansible will be run. The control node is responsible for running the provisioning on the servers you are managing.

Managed Hosts

Managed hosts are those servers where you are going to provision or simply the nodes/servers which you are going to control through ansible.

Inventory

An inventory defines a collection of hosts that Ansible will manage. These hosts can also be assigned to groups, which can be managed collectively.

Playbook

A playbook is a text file containing a list of one or more plays to run in a specific order. A playbook is a text file containing a list of one or more plays to run in a specific order.

Play

A play is an ordered set of tasks run against hosts selected from your inventory. A playbook is a text file containing a list of one or more plays to run in a specific order.

Before Writing Playbook

Make sure you have installed ansible on the control nodes. For ansible installation, you can follow the official documentation of ansible.

Connection Setting: By default, Ansible connects to managed listed in inventory hosts using the SSH protocol. Ansible uses the value of the remote_user parameter to connect managed hosts with the specified user. For setting up connection you can either install the public key on managed hosts and populate your local ~/.ssh/known_hosts file with its host key or you can use the ssh key-pair file with .pem extension.

We will be requiring a project directory from where we can run ansible commands. Create a new folder on your control node and create the following files in that directory:

  1. ansible.cfg

This file needs to be located in the current directory where ansible is being run from

2. inventory

The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. Add the IP Address of hosts that you want to manage through the playbook.

Let’s not hold things up any longer and understand the concept of playbooks better through some simple examples.

Writing Simple Playbook

Create a file called sinple_playbook.yml and add the following content into it with proper indentation:

Let’s understand the playbook written above:

  1. A playbook begins with a line consisting of three dashes (- – -) as a start of document marker. . It may end with three dots (…) as an end of document marker, although in practice this is often omitted
  2. The line after dashes begins with a dash and starts the first play.
  3. Now according to YAML syntax here we have three keys as they all have the same indentation

a. name

This key is optional but recommended to identify what the play is for (ex: This play installs software packages on managed hosts).

b. hosts

This attribute signifies the host or list of hosts against which the play’s tasks are run (ex: This play will run on the server group from the inventory file).

c. tasks

This attribute signifies an ordered list of multiple tasks that will be executed. (In the above playbook two tasks will get executed)

Verifying Playbook Syntax

It is good practice to perform a verification to ensure that the syntax of its content is correct. For this, you can use the ansible-playbook command with –syntax-check option to verify the syntax of the playbook. If our syntax is correct, the command will return us the playbook name in return with no failures.

Let’s verify the playbook that we have written:

When syntax verification fails, a syntax error is reported and we can get detailed output and exact location of syntax issues in the playbook.

Edit the playbook and remove ‘s’ of key ‘hosts’ in line 3 of the playbook. Verify the syntax of the playbook again.

Now let’s verify syntax again and see the output that we get:

The command returned as a failure message with the exact error and location of the error.

Go ahead and reverse our mistake so that the playbook should be executable without any syntax errors.

Finally, Running Playbooks

Now we execute the command to run the playbook. ansible-playbook command is used to execute the playbook on the control node and the name of the playbook to be run is passed as an argument.

Writing Multiple Plays

A playbook is a YAML file containing a list of one or more plays. Remember that a single play is an ordered list of tasks to execute against hosts selected from the inventory.

We can implement a playbook that container multiple plays, each play may apply tasks to a separate set of hosts and this is very useful when applying specific configurations which may involve different tasks on different hosts. You can write a playbook that runs one play against one set of hosts, and another play against another set of hosts.

The following example shows a simple playbook with two plays run against two different host groups.

Update inventory file in the project directory and multiple managed hosts into it.

Now, write a playbook with multiple which will run against the different hosts and install two different webservers.

Check the syntax before running the playbook. Run playbook and observe the output. The first play runs against webservers1 which installs Nginx web server, and the second play runs against webserver2 which installs Apache webserver.

Hope it was informative and you liked it.

--

--