Configuring reverse proxy and updating its configuration file automatically using ansible-playbook

What is reverse proxy?
A reverse proxy is a server that sits in front of web servers and forwards client (e.g. web browser) requests to those web servers. Reverse proxies are typically implemented to help increase security, performance and reliability.
What is load balancer ?
A load balancer can be deployed as software or hardware to a device that distributes connections from clients between a set of servers. A load balancer acts as a ‘reverse-proxy’ to represent the application servers to the client through a virtual IP address (VIP)
What is HAProxy ?
HAProxy is a high-performance, open-source load balancer and reverse proxy for TCP and HTTP applications.

Task is to configure HAProxy using ansible-playbook:
In inventory there will be one ip as load balancer and others for backend.
Here, [loadbl] is for load balancer and [backend] for backend servers.

For backend, we just need to do same steps as we do for httpd setup
- Install httpd and php using package module
- copy html file using copy module
- Start service
- Enable firewall for port.
here, I have adding steps for mounting dvd and yum configuration because I had new os launched, that means this playbook can work on newly launched os no need to do anything manually other than launching os.



For load balancer,
Whenever we want to setup a proxy server using HAProxy , first we need to install HAProxy ,then we have to add ip addresses of backend servers in the backend section of configuration file of HAProxy.
configuration file of HAProxy is /etc/haproxy/haporxy.cfg
For automating updation of ip whenever a new ip is added in inventory we will use for loop and jinja in source file and also put port 8080 in bind*:

In jinja for “loops” we have {% loop %} syntax
Here, groups is inbuilt variable in ansible to apply for loop to all backend host. And loop.index gives index to server like app1, app2 and so on ,{{ i }} i will be for ip so that wherever for loop found ip in [backend] it will iterate them one by one here.
Also, in jinja for “for loop” we have to end for loop , like {% endfor %}

Now we need to give this file as source to templete module to automate HAProxy.cfg file in proxy server.
Steps for load balancer: (in same playbook)
- Install HAProxy
- Updating configuration file of HAProxy.
- Starting HAProxy service.

Run playbook and then check for website using proxy server ip:

Here, we can also use php to show backend server ip but i have used html .


Successfully completed our task!!!!!
Hope you all like it!!
KEEP LEARNING
THANK YOU