Configure the Haproxy(Load balancer)and the Web Server Using Ansible Roles
Hello Guys !!
In this article I will gonna to configure the load balancer and webserver using the ansible roles , in such a way that if in future any backend web server come up then it will automatically get registered in the haproxy configuration file..
So, at first I created a folder name workspace using mkdir workspace command, as it sounds it will be our workspace..
In this workspace folder I created three roles
- webserver -: For configuring the web server
- loadbalancer -: For configuring the load balancer
- cdrom-and-yum -: General role for creating drive and mount it to cd-rom and configuring the yum

In the cdrom-and-yum role I write some tasks
- Created a folder
- mount it to the cd-rom
- configure the yum by creating the repos

In the above main file of the task folder in the cdrom-and-yum role , I made the use of variables
- dvd -: contains the path of the drive
- repos -: It is a list containing the name of the folder in the cd-rom which are the repositories for yum i.e AppStream and BaseOS one
the main file of vars looks like

Then I created some tasks in the weberver role
- Download the httpd or apache2 software according to the os respectively
- configure the server
- upload the webpages in the document root of the webserver
- starting the service accordingly


In the above main file of the task folder in the webserver role I made the use of notify-handler so that the service gets started only if some changes is done by the template module in the configuration of web server . Also , I made the use of several variables ..
My variable file looks like

And handler file looks like

Also , the configuration file(which is uploaded by the controller node to the target node in /etc/httpd/conf.d/ folder) looks like

And the uploaded web page is

Now , I created the tasks in the loadbalancer role

The template file looks like, (I am showing only the changes, the rest file is the same)

Finally , I created the main setup.yml file , which I run with ansible-playbook command
The setup.yml files look like

Finally , I run the setup.yml file ,all the tasks run successfully..
After, the playbook runs completely, I tried to connect to load balancer at the port 8080 using http protocol and I get

In my case the haproxy server itself was a web server(as I was not able to launch one more vm because of limited ram), that’s why is showing the haproxy ip(192.168.43.117) alternatively..
Thanks…..:)