Ansible to configure reverse proxy

Ashish Dwivedi
3 min readMay 23, 2021

Placing a load balancer in front of your web server infrastructure helps ensure any spike in traffic doesn’t bring down the site.

What is a load balancer? A load balancer is an efficient way to distribute the network traffic among various backend servers. It is also known as a server farm or server pool. It distributes client requests or network load to target web servers. Load balancers work on the round-robin concept, which ensures high reliability and availability.

Steps to configure HAProxy

Step 1 — Install HAProxy

To install HAProxy, you have to use a package module where you give the name of the service you want to install:

Step 2 — Copy the configuration file for the reverse proxy

Copy the configuration file so that you can modify it:

Now if we don’t have configuration file or we can say our load balancer is on other OS or we want to configure our haproxy on remote node then we can use ansible Fetch module this module will fetch the file from remote and will store in our ansible master node

Step 3 — Change frontend port and assign backend IPs

By default, the frontend is bound to port 5000. I changed the port number to 8080. I also applied a for loop to configure the backend IP. Now we can launch as many web servers as we need, and there is no need to manually configure the IP inside the /etc/haproxy/haproxy.cfg. It will automatically fetch the IP from inventory.

Here [web] is the nodes where we have configured web server

Step 4 — Copy haproxy.cfg to the managed node

Using template module, copy the config file for HAProxy from the controller node to the managed node:

Now we have to install httpd web server

Step1 — The package module is used to install httpd on the managed node:

Step 2 — Copy the webpage

The template module is used to copy our webpage from the source to the destination:

Step 3 — Start the service

The service module is used to start the httpd service:

We have to install php also in the webserver nodes because our web page written in php.

Complete the playbook to configure the reverse proxy

Output

The playbook runs successfully, and the two target web servers can be accessed by the main web server using a load balancer.

In our web page we had written code for retrieving ip and here we can see we are searching with ip of load balancer but when we refresh we can see ip of both the backend server.

Conclusion

The load balancer and reverse proxy have now been configured by Ansible. You can add a layer of protection and availability to your web services by adding HAProxy to your infrastructure. Be sure to check out the documentation for your specific target to learn more.

IF still not available to connect load balancer then run these two commands in all the targetnode with or without ansible

COMMANDS
1. systemctl stop firewalld

2. setenforce 0

Happy learning……….

Github URL - https://github.com/A-zish/Cinfiguration-OF-HAproxy-with-Ansible.git

--

--