Haproxy LoadBalancer On AWS Cloud with Ansible

Mihir Patel
3 min readMar 19, 2021


👉 HAProxy, which stands for High Availability Proxy, is a popular open-source software TCP/HTTP Load Balancer and proxying solution which can be run on Linux, Solaris, and FreeBSD.

In this blog .. You will see LoadBalancer on top of AWS cloud and Automate the LoadBalancer With the help of Ansible Playbook so We need to Only one click and Entire Setup will Automatic Generated ….

Lets, make two instances on AWS Cloud One Instance for Load balancer and another for Backend webserver .

We can make this setup by the manually , but it can take More time so In this situation we use Ansible-Playbook Which make Entire setup in one-click .

First we need to install ansible in AWs Cloud .

Step 1: Ansible work on Python So first install Python .

step 2: the pip install ansible .

pip install ansible

step 3: before install ansible some dependencies require in AWS Instances ,

For this install below rpm file .

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm 

If you have redhat verion ≥8 then install below rpm file

# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

step 4: Now see ansible version

Note :If you have ansible config file=None then ,create the directory /etc/ansible/ansible.cfg , so Ansible automatic fetch this path for config. path .

Step 5: Now Make ansible-playbook …

hear one hosts — webserver will configure the webserver with respected ip’s and —loadbalancer for configure the loadbalancer with respected ip’s . ip’s are writen in Inventory file .

This is Inventory file

In this Playbook Index.html Which is Our website And this Run in web server and top of webserver we have Load Balancer So , If we want access the website We need to use Only LoadBalancer IP:Port , SO we can make Our Website Secure.

Furthermore , If Many Request come on LoadBalancer we can make Many webserver behind LoadBalancer So we can Balnce Load .

For making dynamic Haproxy.cfg File We need to use Jinja code in haproxy.cfg , bcz if We add one ip in Inventory then automaticlly attach with LoadBalancer .

Now Run the Playbook

Here Load Balancer IP is :

and webserver ip is :

SO access the Website use Loadbalancer IP:Port

✨Now successfully run Website with LoadBalancer….✨



Mihir Patel