Ansible Roles: Configuring Webservers & LoadBalancer
Tags: ansible, devops, webservers, loadbalancer
Published: 2021-03-28 15:40:11

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7wbxoltwn6s26etkmuq7.png)
##Creating Roles

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

    ansible-galaxy role init <<role_name>> 
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3di87a9zp2xdtayldmb6.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdqvg6mbcz1f42txy8ox.png)
Now change to the directory /tasks and write the following code in main.yml.
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s5jd2g4xk4ghkn9312tf.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8jbi0qbh74ig6cpt9clf.png)
Here, We are done with the configuration of the webserver.

This will,

    Install httpd software.
    Start the httpd service
    Copy the webpage.

Now, we will create a role for Configuring the load balancer.
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f28x1kjkv6bble7176jh.png)
Here I have created the role named MyLoadBalancer which consists:
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhom9stux6pthubquouk.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31wk90xe4naufkzjs52q.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mrcgwv4wo5haht978yb.png)
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2q5zfadklaig5njw23w1.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hikhmzba01q19dk6f91.png)
The code in main_setup.yml is as follows:
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwmv8huod3u9jh4tbsy9.png)
Now let us run this playbook using:

   ansible-playbook main_setup.yml

We can see the execution :  
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zwcht0j7b5y87lwvmyvv.png)
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gv530xx37bqnb002akjw.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lk3rrf7xoxkj9iepjd0s.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p3ojakdx8o4m52m2bxeq.png)
That’s all We did it.
![alt text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j96xo5prr9yhujyew80.png)
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