How To Configure the haproxy using the Ansible and also how to Configure Haproxy Dynamically.

Gaurav Khore
6 min readJan 22, 2021

What is Ansible?

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration.

What is Haproxy?

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers.

Lets see an example of how to configure haproxy using ansible as an automation tool:-

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

Lets See the solution:-

We will do the above configuration on the linux os.

Step1:-Install the ansible in your base os:-

For installing the ansible use the following command i.e “pip3 install ansible”

For checking the ansible version use the following command i.e, “ansible — version”.

Also create a configuration file of ansible in the “/etc/ansible/ansible.cfg” location.

Step 2:-Create a Inventory file:-

Inventory file:- Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.

For the above use case create inventory file as shown below:-

ip.txt is the inventory file and on the controlnode ip’s we will configure the haproxy and on the targetnode ip’s we will configure the webserver which will be managed by the haproxy for load balancing.

Also check that the host (i.e ip) provided in the ansible inventory file are pinging or not for that we can use the following command:-

ansible all --list-hosts

Its output will be like:-

Step 3:- We will create a jinja file for the haproxy.cfg file:-

Copy the haproxy.cfg i.e configuration file for the haproxy into the current working directory with name as “haproxy.cfg.j2”. We generally use “.j2” extension for the jinja language. we are doing so because we have to create the haproxy configuration file dynamically i.e if we add ip in the targetnode of the inventory file the haproxy configuration file automatically get updated .

We will write the jinja code in the haproxy.cfg.j2 file because ansible has a template module that has intelligence to execute the file before copying the file on the target node. The code in the haproxy.cfg.j2 will be like:-

Step 4:-Write an Ansible playbook :-

In this we will write ansible playbook that will first configure the haproxy on the controlnode and after that it will configure the webserver on the target node.

For install haprocy we have to use the following statement in the ansible playbook i.e,

- name: "install haproxy"
package:
name: haproxy
state: present

Also we have to give make the haproxy configuration file dynamic in nature so for that we will use the template module of the ansible and copy the haproxy.cfg.j2 file i.e file created in above step on the target node so for that we will use the following statement in the playbook i.e,

- name: " configure haproxy.cfg file"
template:
src: "/root/ansible_ws/ans_tas_12/haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"

After that we will start the haproxy service for that we will use the service module.

On the target node we have to configure a webserver for that we can use the following statement in the ansible playbook i.e,

- hosts: targetnode
tasks:
- name: "Installing httpd on the target node"
package:
name: httpd
state: present
- name: "copying data to target node"
copy:
dest: /var/www/html/index.html
content: "hi....From the target node"
- name: "Httpd service start"
service:
name: httpd
state: started
enabled: yes

Our ansible playbook be like,

- hosts: controlnode
tasks:
- name: "install haproxy"
package:
name: haproxy
state: present
- name: " configure haproxy.cfg file"
template:
src: "/root/ansible_ws/ans_tas_12/haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"
- name: " haproxy service start"
service:
name: haproxy
state: restarted
- hosts: targetnode
tasks:
- name: "Installing httpd on the target node"
package:
name: httpd
state: present
- name: "copying data to target node"
copy:
dest: /var/www/html/index.html
content: "hi....From the target node"
- name: "Httpd service start"
service:
name: httpd
state: started
enabled: yes

Step 5:- Run the above anisble playbook:-

For running the ansible playbook we can use the following command i.e,

ansible-playbook lb_hapro.yml

Output will be like,

step 6:- Changes made by running the ansible playbook on both the targetnode as well as on the conrollnode:-

On control node haproxy configuration file will be like, the file dynamically configured for the ip of the target node of the inventory file.

On the target node httpd server configured successfully,

In the above image we can see that the port number 80 has started and also the index,html file has been successfully writtend.

In the above image we can see that the httpd server i.e apache webserver has been successfully deployed on the targetnode.

Step 7:- Web page access from the target node:-

We can use the ip of the controlnode to access the webpage running on the target node with help of the load balancer and reverse proxy offered by the haproxy, For access the web page we can use the following url syntax:-

http://ip_controlnode:port_haproxy

in our case the url will be like:-

http://192.168.1.6:8080

Step 8:- Now we have made our playbook dynamic so after adding one more IP of Backend Server in Inventory File we can follow all the steps again so that dynamically its IP will add in the haproxy.cfg.j2 configuration file and all other tasks assigned in the playbook.:-

Inventory file be like:-

We can check the connectivity to the target node of the ansible by pinsing them all for that we can use the following commands,

ansible all -m ping

Output will be:-

Without making change in the ansible playbook just run it once again, while running the playbook ansible only make change in the haproxy.cfg file and the newly added target node,

Output will be like:-

After running the ansible playbook again the hadoop.cfg file has made changes and it will be like,

On the target node the apache webserver has been successfully configured, we can see by ,

The web page will be like the same and it can access the same way as we have done previously,

The ip of the target node has dynamically added in the haproxy configuration file successfully..

Thanks For Reading……..

--

--