Configure Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory on AWS using Ansible-playbook

Pratyush Pathak
3 min readMar 24, 2021

Let’s start!

To perform this task, first we will have to configure the ansible inventory file and ansible.cfg file

vim /etc/ansible/ansible.cfg

Now as we have to perform this task on AWS, so first we will have to launch 3 instance on AWS:

1 for Haproxy and 2 for the web servers

vim aws.yml

Here we have to run it on localhost,

You need a Access_Key and Secret_Key

ansible-playbook aws.yml

You can cross-check your aws instance page:

Now, we have to update the IP in the host file

vim /etc/ansible/hosts

You can see here;

Now 3 hosts are ready one for the Load Balancer and another 2 for the web servers

Listing the hosts:

ansible all — list-hosts

Now we can check the connectivity using ping command:

ansible all -m ping

Now, Lets start writing the ansible playbook

For web servers:

vim aws_haproxy.yml

ansible-playbook -v <file_name> you can use -v (-vv, -vvv, -vvvv ) for more information

ansible-playbook aws_haproxy.yml

Now we will further write the playbook for the haproxy server

To setup a Haproxy LB we have to perform three steps:

step 1: Install the software

step 2: configure the /etc/haproxy/haproxy.cfg file and update the backend app Round-Robin IP

step 3: start the services

For step 2, I have updated the configuration file using jinja framework so that it will be automatically updated if any new managed node will come in future, and I’ll copy the file to that location (i.e., /etc/haproxy/haproxy.cfg) using template module

You can download the code from here: click here

Now the playbook for LB

ansible-playbook aws_haproxy.yml

Now we can check the Haproxy Load balancer is configured or not

http://myLBip:8081

Done!

Code: https://github.com/pkpathak143/aws-haproxy-ansible-playbook

Thank you!

--

--