Ansible is a popular server configuration management tool that lets users manage and monitor remote systems from a single control node. With Ansible, you can install software packages, deploy services, and make configurations on multiple hosts from a single node instead of logging into each of the nodes. We already have a guide on how to install and set up Ansible on Ubuntu 20.04. This will give you an introduction and a likely head start as you go over this guide. In this guide, we will narrow the focus to what playbooks are, how to create them, and use them to deploy services.
We already have a home lab as provided below. To get the most out of this guide you can replicate it or have a similar lab environment on a virtualized platform:
Ansible control node IP: 192.168.2.106
Managed host IP: 192.168.2.108
With the setup in check, let’s get started.
What is a playbook file?
Ansible, just like Terraform, falls under the Infrastructure as a Code. What does this mean? Infrastructure as a Code (IaC) is described as a mechanism of provisioning and managing hosts using machine-readable configuration files as opposed to physically logging in and making the configurations. In Ansible, a playbook is one such configuration file.
A playbook is a file in YAML that contains one or more plays. What is a play? A play is an ordered task that automates a task or process on the managed host such as deploying an application such as a web server or making configurations. A playbook can have one or multiple plays, each performing different tasks.
Plays make use of modules which are special functions to specify the changes required on the remote host. Each module is special and defines a particular task.
A playbook file is saved with a .yml or .yaml file extension.
Creating a playbook file
Let’s now create a playbook file. In this demonstration, we will create a playbook file called greetings.yml in the Ansible directory path /etc/ansible as shown.
$ sudo vim /etc/ansible/greetings.yml
Add the following configuration. This is a simple playbook that prints a message to stdout on the remote server. Take careful note of the indentation of the modules.
The Ansible Playbook file begins with three hyphens ( — ) to indicate that it is a YAML file. The ‘hosts’ parameter specifies the remote host or group of hosts defined in the inventory file, which by default is located in /etc/ansible/hosts. Here, staging is the host group for which the remote host of IP 192.168.2.108 is defined.
The remote host is defined under the host group called staging with the following entries.
[staging] 192.168.2.108 ansible_ssh_pass=xxxxxxxx ansible_ssh_user=jack
The ansible_ssh_pass specifies the SSH password of the remote user while ansible_ssh_use specifies the user name on the remote host.
Next, we have the name of the play “Print a simple message” followed by the debug module that prints out the message defined by the msg module.
Executing the playbook file
To execute the playbook, simply use the ansible-playbook command in the syntax provided below.
$ ansible-playbook /path/to/playbook-file
In our case, this is going to be:
$ ansible-playbook /etc/ansible/greetings.yml
During play execution, Ansible first prints out the name of the host group or remote host on which the play will be executed -in our case the staging group. Ansible then retrieves information about the play, referred to as Facts, and finally performs the action specified in the playbook. Here, the simple message is printed.
Let’s take yet another example of a playbook file called install_apache_and_git.yml as shown below. Here, we have two plays. The first play installs the Apache webserver while the second play installs git on the remote system. The become: true parameter executes the command as an elevated user or sudo user on the remote user as is expected.
When the playbook is executed, all the plays are listed in order of execution from the first to the last. The playbook first installs the Apache web server before installing git. The –ask-become-pass directive prompts for the sudo user in order to carry out the tasks defined in the plays.
And that’s how you can create a simple playbook file and execute it. We hope that this provided a basic level understanding of an Ansible playbook file, its structure, and how you use it to carry out tasks on remote hosts.