Configuring Ansible Playbooks
Manuj Aggarwal
Top Voice in AI | Helping SMBs Scale with AI & Automation | CIO at TetraNoodle | AI Speaker & Author | 4x AI Patents | Travel Lover??
What is an Ansible Playbook?
Let’s learn about this brilliant yet easy feature of Ansible.
Ansible Playbooks are an innovative method to run complex commands on multiple virtual nodes. Entire environments can be configured and managed using Playbooks. Playbooks are written in the easy-to-use YAML format. A Playbook in Ansible is a set of plays that are designed to perform an action on specific nodes. You can effectively configure and automate a large number of plays without having to execute them individually.
A play includes tasks, which is a set of actions designed to achieve the goal of the play. Plays that are configured in a playbook are sequentially implemented. You can configure multiple tasks and sub-tasks within a specific play. You do not have to worry about creating separate plays for similar actions, because they are highly flexible and reusable.
A common use case of Ansible Playbooks can be this: an unprivileged user can manage content in their home directory with Ansible, even if they do not have root or sudo privileges on their machine.
How to Create an Ansible Playbook?
Creating and configuring a playbook is very simple.
- Let’s open the Visual Studio Code workspace and create a new playbook. Let’s click the New File icon and name it it as install_git.yml. That’s all that is required to create a new playbook.
Let’s now configure it by adding plays and tasks.
- In this instance, you shall see how to install git on the Ubuntu server. Now, to follow the convention of a YAML file, you must enter 3 hyphens on the first line of the file. This is the easiest way to identify YAML files too! The commands required for configuring the play are quite simple and straight forward too!
Let’s open this playbook and create a play. Again, the process of creating plays is very simple. On a new line, simply enter a name, followed by install git on ubuntu. If you remember, in the Inventory section, the nodes in the inventory file were configured as Ubuntu and Fedora. You are going to see how to use these two nodes for installing git. Next, you must configure hosts for this play. Enter the hosts as ubuntu, since this play will install git on the Ubuntu server. Now, this play requires passwordless root privileges enabled on the remote servers. Let’s enter the become key, followed by True.
- The next action is to define tasks for this play, enter the tasks key on a new line. Multiple tasks must be listed in the form of arrays, each task on a new line, following a hyphen. Let’s enter name of the task as install git. Each task calls an Ansible module for execution. Next, you can enter the package manager for the Ubuntu server, which is apt. Followed by the name of the package, name equal to git. That’s it, the playbook is now ready to be executed!
- Let’s execute the playbook via the integrated terminal with this command, ansible hyphen playbook, followed by the name of the playbook which is install_git.yml, and lastly, the inventory flag, hyphen i inventory. Quite direct commands for this playbook, as you can see. You can view the progress of installation on the terminal. The execution gathers facts about the remote server, such as CPU space, RAM speed, etc. The changed status of the task shows that git is successfully installed on the Ubuntu server. This is how simple executing a playbook is. Imagine all that you can achieve with a correctly executed playbook! So many complex operations can be simplified quickly!
Implementing Variables in the Playbook
Ansible uses variables to provide flexibility in playbooks. Its benefits include the ability to loop through a set of values, access information like system hostname, etc. However, there are some restrictions for Variables. Variable names should include letters, numbers, and underscores. And they should always start with a letter.
You can easily configure variables in the playbook by including a vars key or importing an independent variable file into the playbook.
- In the existing playbook, one of the ways to use variables is by adding the varskey, as we had discussed. For this, let’s provide the variable name as package, followed by the package name, that is git. Edit the data in the apt package manager line, under Tasks. Now, let’s delete git and follow the syntax to enter the package value in this line. The syntax is simple too. After the equal to sign, type double quotation marks and enter double curly braces inside them. Inside the double curly braces, enter the name of the variable, that is package.
Upon executing the playbook with the same playbook command, you can see that the variable was successfully implemented.
In this way, you can easily add multiple variables to a play and execute it in a single playbook. This technique is quite useful when you want to avoid the confusion of multiple redundant playbooks.
Another way to implement variables in playbooks is by creating a variable file, and then adding it to the playbook for execution. This is also quite simple, as you only need to create a separate file with the variable name and value. And then include it in the playbook.
- Let’s start by creating a new file and naming it as variable.yml. The .yml extension, as you know, is to denote that it is a YAML file. Let’s add three hyphens on the first line, and proceed. Let’s enter the name of the variable as package, followed by value as git. Save this file and go back to the install_git playbook.
- To include the variable file in this playbook, it must be specified in both the plays. Let’s start with the first play. Let’s edit the vars key to vars_files. In case of multiple files, you can list them as an array. To enter the name of the variable file, on the next line, type hyphen variable.yml. Let’s go ahead and save this file. Now, execute the playbook command. Again, the execution was successful and git was installed on the remote hosts. It’s that easy!
In this way, you can easily create multiple variable files and list out all of them in a single playbook. This approach is quite effective for your day-to-day automation functions.
Implementing Conditional Statements in the Playbook
Conditional statements are used to inform the program what commands to run, based on certain conditions.
If-Else Statements are conditionals that help control the execution flow of plays in Ansible. These can be used with the Boolean condition of Yes/No and True/False.
The When Statements in Ansible are used to perform actions only on a few of the configured hosts. It helps you skip steps in a play, so that you can execute only the necessary tasks.
Using conditional statements, you can install the git package on specified hosts. Let’s see what conditions you can use here.
- Let’s use the git installation playbook for this topic. Below the apt package manager line, you need to enter the when statement followed by ansible_os_family, two equal signs, and then Debian in double quotes. Let’s understand this syntax. The double equal to sign is used to denote a condition. This condition means that git should be installed on the remote host only if the operating system belongs to Debian. That’s all, let’s execute the playbook.
You can see that the git package was successfully installed on the Ubuntu server, as it belongs to the Debian family.
Conditional statements are effective tools to reduce the playbook configuration efforts. You can control the entire execution flow of the playbook, signifying only specific tasks and plays according to your requirements. They ensure quick execution and do not affect the efficiency of automation. This again, benefits you when you are responsible for complex automation operations in your development projects.
If you want to learn Ansible from scratch then our training program “DevOps: Hands-On Guide To Automation With Ansible“ is for you.
These DevOps technical skills are in high demand, but there’s no easy way to acquire this knowledge. Rather than rely on hit and trial method, this course will provide you with all the information you need to get started with your Ansible and DevOps projects.
What are you waiting for? Just click on the “Add to cart“ button. It’s time to take action. Become an expert DevOps engineer and generate a more significant income for you and your family.
Still not convinced? Take a look at our “Beginners’ Guide To Automation With Ansible”It is absolutely free of cost, and you will get a gist of what you will be gaining from the main course.
Let’s do this together!