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.
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.
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:
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.
For creating roles, We need to run the following command,
ansible-galaxy role init <<role_name>>
This directory contains the files or configuration files which we need to use. Change to this directory and keep your webpage there.
Install httpd software. Start the httpd service Copy the webpage.
Install the haproxy software
configure the haproxy.cfg(configuration) file
start the haproxy service
In haproxy.cfg file write the following:
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.
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.
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.
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.
That’s all We did it.
For your reference I am Linking my Github repository here.