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:
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.
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:
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.
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:
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:
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. Put the below YAML code in this file and save the file. 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: Now, I will run the ansible playbook, which I create to install and start nginx. Run the command below to check if the nginx server was installed and started correctly by the ansible playbook. 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.