Haproxy with Ansible automation

Deepak Kumar Pandia
4 min readMar 20, 2021

In this article we are going to see , how we can deploy a loadbalancing setup with the help of haproxy and Ansible on the top of one of the biggest cloud AWS.

Haproxy is a tool which provides the facility of loadbalancing in very fast and high availibility manner.

why Ansible?

Ansible automates and simplifies repetitive, complex, and tedious operations. in todays automation world Ansible is growing alot because it brings huge time savings when we install packages or configure large numbers of servers. Its architecture is simple and effective.

Ansible roles makes Ansible a better choice because roles provides facility to use the same code multiple times in multiple playbooks independently, so it will save our time , we can directly use roles instead of writting the code again and again.

command for creating the role:

ansible-galacy init <role_name >

in this setup i am going to create two roles:

  1. myapache: this role will configure the nodes as backend servers and install all the prerequisites like httpd webserver and php. https://galaxy.ansible.com/deepakad903/myapache_ansible_role
  2. loadbalancer : this role will install Haproxy and it will automatically update the config file of haproxy whenever a new node comes up in the inventory. https://galaxy.ansible.com/deepakad903/ansible_haproxy_loadbalanceras you can see by clicking on the link provided for loadbalancer role, this role will replace our configured haproxy.conf file with the default config file of ansible in our managed node. in this file we have provided a loop for updating the ip of managed nodes.

as i mentioned above i am going to use AWS for provisioning the instances so i have to update the config file of ansible so my ansible can go to the AWS cloud and can do further tasks.

/etc/ansible/ansible.cfg is config file for ansible

in this file we can update out default roles location, keys/password of nodes etc.

now we are going to create an ansible playbook which contains both the roles. this playbook will provision the instances first on AWS cloud and than it will do further configuration.

this is the whole ansible playbook for the entire setup. as you can see this ansible playbook contains a secret file, which contains the credentials of my AWS account. ansible provides a facility to secure our data in encrypted form.

now we can run this playbook with the help of this command:

ansible-playbook --ask-vault-pass haproxy.yml

when we run this command this playbook will prompt us two times

  1. to enter the password of our secret files
  2. to enter the number of backend servers to be launched

as you can see 3 instances are launched one with the tag name loadbalancer and two instances with the tag name webserver.

after provisioning the instances this playbook will wait for 30 seconds so our instances became ready to use. after provisioning the instances this playbook will configure the instances according to our need.

as you can see playbook run successfully now if you want than you can check is everything working fine with the help of loadbalancer IP and port number 5000( because haproxy bydefault works on port number 5000).

as you can see our setup is working fine , everytime when you refresh this page it will show us ip of the backend webserver’s instance.

CONCLUSION: we have created a dynamic playbook which will ask us to enter the number of backend webservers , rest of the entire setup will be done automatically with idempotence nature.

THANKS FOR READING …….

--

--