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

Gaurav Khore
8 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.

What is AWS EC2 ?

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. Amazon EC2’s simple web service interface allows you to obtain and configure capacity with minimal friction.

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. Do this setup on the AWS Ec2 instances.

Lets See the solution:-

We will do the above configuration on the linux os.

Step 1:-Launch 3 AWS instances:-

For the above task we have to launch 3 aws instances having the awi (i.e amazon machine image) as redhat linux ami and also attach the security group which allows the port number 22 for ssh and also port number 80 for apache webserver and also prot number 8080 for the haproxy sercer.

For launching the aws instance using command line interface go through the below mentioned url:-

Step 2:-Install the ansible on your base os:-

Before installing the ansible we have to install the python3 because ansible uses some libraries of the python, for that we can use the command,

yum install python3 -y

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. Also in the configuration file we have to provide that the command run while running the ansible playbook on the aws instance should be run with the root power for that we have to give it the “privilege_escalation”.

Also provide the inventory file location.The configuration fille will be like,

Step 3:-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.

In the ip.txt file we have to provide the location of the ssh private key (.pem format) to the inventory file because the ansible connect to the target node using the ssh protocol and ansible will connect to the instances using the ssh key provided inside the inventory file.

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 4:- 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 5:-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 6:- 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 7:- 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 8:- 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://public_ip_controlnode:port_haproxy

in our case the url will be like:-

http://13.233.12.124:8080

Step 9:- 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……..

--

--