Task-12.1: Ansible Related Task.
Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.

Pre-Requirements: Linux System( We using RHEL-8)
a. Ansible Controller node
b. Haproxy for Load Balancing
c. Apache Httpd webserver
What we going to do?
We will be Configuring 2 Nodes as “httpd webserver” and Load Balancing will be managed by “haproxy server” and all this is done using Ansible by writing a “playbook”.
Lets Get started:
♦ Step-1: Adding Jinja code (Templating) in haproxy config file
>> Go to : cd /etc/haproxy/haproxy.cfg
>> Adding the Jinja code which will be used by Template module in Ansible
>> Changing Port to 8080 (At your Preference)

>> Adding loop in the config file to add All the Node IP’s which are to be configured as Webserver.

♦ Step-2: Creating a Simple php code for testing later.
>> Create these file in the directory where you going to run the Ansible playbook.
>> Create a Php file to be read by Webserver: vim index.php
Edit as Follows
<pre>
<?php
print “/usr/sbin/ifconfig”;
?>
</pre>
♦ Step-3: Creating a playbook to configure everything.
>> We are configuring 1 Load balancer and 2 Webservers
>> First configuring the loadbalancer:
>> Installing haproxy
>> Adding haproxy.cfg file we modified earlier using Template module
>> Restarting the service
>> Modifying the Firewall rules

>> Changing the Selinux permission to allow the use of ports
>> Lastly, Configuring the Webserver:
>> Installing httpd
>> Installing php
>>Adding the “index.html” file in the default folder of httpd webserver: /var/www/html

>>Restarting the httpd service

♦ Step-4: Running the Playbook.
>> Using the following command:
ansible-playbook <Playbook name>



♦ Step-5: Testing our loadbalancer.


Everything is working fine and load is balanced by Round-Robin Method.
Hope you guys Enjoy.