Using Ansible playbook : Configure Reverse Proxy (Haproxy)and Configured With Apache Webserver

Anil Yadav
3 min readMar 22, 2021

ARTH — Task 12 👨‍💻

Task Description 📃

12.1 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.

In this Task, We have a Controller node and Three Target Node is configured. One Target node as a Haproxy and two target node as a Webserver In the Controller Node use “pip3 install ansible “ command to install Ansible.

A (Work as a Haproxy Device)→ IP(192.168.0.107)

B ( App Hosted Machine) →IP(192.168.0.109)

C (App Hosted Machine) →IP(192.168.0.110)

Pre-Requisites: Configure yum !

Now, let’s create an inventory file in Controller node : use : “vim ip.text” command And write Information about Target Node like, IP Address, user name , passwd.

We need to specify the path of inventory file in the /etc/ansible/ansible.cfg file.

Now, list the hosts using following command: “ansible all — list-hosts”

Command : ansible all -m ping

Configure A and B machine and also we are going host an index.php page which will show the output of (ifconfig command) on page…

Then we have to create the variable file to make it dynamic :

Command : vim var_file_name.yml

Create haproxy.cfg file using following command add the following code in that file and save it :

Here I am using jinja2 embedded code for dynamically fetch or register the new webserver IP with HAproxy loadbalancer.

Command : vim haproxy.cfg

Create an Ansible playbook for performing the practical using the following command add the following code in that file and save it :

Command : vim file_name.yml

Now, we have run the Ansible Playbook by using following command :

Command : Ansible-playbook file_name.yml

Now, let’s check the outcome by visiting the HAproxy server IP with HAproxy port number .

here have you notice , the IP is switching even though the IP address which I am using at browser is same. This proves that the HAproxy is working fine.

Now the challenge is to configure HAproxy automatically when a new IP is added in the inventory.

So, add one more target node in the [myweb]group. For this, open the inventory file and add a new target node. And run Ansible playbook again you can see that 3 IPs are configure automatically, which means that the third target node is also automatically added in the configuration file of HAproxy.

THANKS FOR READING TILL THE END🤩

keep learning !!!! keep sharing!!!

--

--