Ansible Roles: Configuring Webservers & LoadBalancer

Piyush Bagani - Mar 28 '21 - - Dev Community

Hello Readers😊,

After Reading This Article, You will come to know about Ansible Roles and how they play a vital role in managing the playbooks.

Here I will be creating Two roles, one role for configuring webservers and another for configuring the load balancer (reverse proxy).

To understand more about Ansible, Webservers, and Load Balancer, You can refer to my other blog linked below.
Link: https://dev.to/piyushbagani15/configuration-of-apache-webservers-updating-haproxy-dynamically-using-ansible-2b7h
Let us understand what are roles??

Roles are a way to create multiple tasks together to do automation in a very effective manner with proper directory structures. They allow you to break up the configurations and perform it effectively. They can be easily modified and the syntax errors can be reduced.

Structure of Role Directory

An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role. You can omit any directories the role does not use. For example:

playbooks

site.yml
webservers.yml
fooservers.yml
roles/
common/
tasks/
handlers/
library/
files/
templates/
vars/
defaults/
meta/
webservers/
tasks/
defaults/
meta/

By default, Ansible will look in each directory within a role for a main.yml file for relevant content (also main.yaml and main):

tasks/main.yml - the main list of tasks that the role executes.
handlers/main.yml - handlers, which may be used within or outside this role.
library/my_module.py - modules, which may be used within this role
defaults/main.yml - default variables for the role. These variables have the lowest priority of any variables available and can be easily overridden by any other variable, including inventory variables.
vars/main.yml - other variables for the role
files/main.yml - files that the role deploys.
templates/main.yml - templates that the role deploys.
meta/main.yml - metadata for the role, including role dependencies.

Now let us move ahead and create the role for Webservers.

If you store your roles in a different location than /etc/ansible/roles/, set the roles_path configuration option so Ansible can find your roles.

Here you have to mention the roles_path in the ansible config file.
alt text

Creating Roles

For creating roles, We need to run the following command,

ansible-galaxy role init <<role_name>> 
Enter fullscreen mode Exit fullscreen mode

alt text
Here I have created the role named MyWebservers.
NOTE: Here I am doing this demo by launching instances in AWS cloud, you can do it in a local system by launching multiple VM(s).

The files inside the role named MyWebservers are as shown below.
alt text
Now change to the directory /tasks and write the following code in main.yml.
alt text
files/

This directory contains the files or configuration files which we need to use. Change to this directory and keep your webpage there.

Here I have a file named index.php that is my webpage.
alt text
Here, We are done with the configuration of the webserver.

This will,

Install httpd software.
Start the httpd service
Copy the webpage.
Enter fullscreen mode Exit fullscreen mode

Now, we will create a role for Configuring the load balancer.
alt text
Here I have created the role named MyLoadBalancer which consists:
alt text
Change to tasks/

This directory contains all the tasks which we are gonna use. Edit the main.yml file and write the following code:
alt text
For configuring the load balancer this will execute the following

Install the haproxy software
configure the haproxy.cfg(configuration) file
start the haproxy service

In haproxy.cfg file write the following:
alt text
alt text
Open the haproxy.cfg file in the controller node and bind the port 8080. Also, write the below-mentioned jinja code to update the haproxy.cfg file to load balancer dynamically.

Now we are done with the configuration of LoadBalancer.

Combining the roles Together

For using the roles we need to make a playbook that will run those roles. Here I have created the main_setup.yml file which is the main playbook we are going to run.

Now our final setup looks like this.

alt text
The code in main_setup.yml is as follows:
alt text
Now let us run this playbook using:

ansible-playbook main_setup.yml

We can see the execution :

alt text
alt text
Voila, The playbook ran successfully without any error, That means the Webservers and Load Balancer are configured successfully.

Let us Verify, For this, We will access our webservers using the IP on which Load Balancer is configured and the port number 8080 which we bound in the haproxy.cfg file.
alt text
Here you can see we are connected to 172.31.39.146, which Is one of our Webserver IP . When I will refresh then it will connect to another Webserver having IP 172.31.33.19.
alt text
That’s all We did it.
alt text
For your reference I am Linking my Github repository here.

Thanks For Reading😊.

Keep Learning, Keep Hustling😊

GitHub Link:https://github.com/PiyushBagani15/Ansible_Roles

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .