Deploy HAProxy LoadBalancer in AWS using Ansible in 30 seconds

In this article, we are going to deploy HA-Proxy LoadBalancer on EC2-instance using Ansible.
Why Ansible…?
Ansible automates and simplifies repetitive, complex, and tedious operations. Everybody likes it because it brings huge time savings when we install packages or configure large numbers of servers.
I create Ansible-Roles for launching EC2 instance and deploy webserver and LoadBalancer Service using the Dynamic-Inventory concept.
If you don’t know how to set up Dynamic inventory for AWS, please refer to my previous article.
So Let’s Start…
For this task, I’m creating three Ansible-roles…
- For Launch AWS EC2 instances.
- For launching Apache Webserver
- And one more for HA-Proxy LoadBalancer
To create an Ansible-role, First create a directory /etc/ansible/roles and after that run command…
ansible-galaxy init role_name
1. Launch AWS EC2 instance:-
Code for launching the EC2 instance is below, I’m created 3 hosts for Webserver and 1 for LoadBalancer…
By Default, Ansible doesn’t refresh the inventory in the middle of the running playbook, so we use the meta keyword refresh_inventory in the last of this code.
So, Ec2-Instance is launched…

2. Launch Webserver On EC2 instance:
Now I need to install httpd software on EC2 instance named webserver using Ansible
After running this role, my webserver is configured.
3. Configure HA-Proxy LoadBalancer:
Now I need to configure my load-balancer service on ec2 instance named lbserver.
Now, all roles are created successfully.
Now I created one playbook for running all roles in a single click…
Save this playbook as lb.yml and then run…
ansible-playbook lb.yml
Finally, LoadBalancer is configured in a single command…✌
Let’s see the output of this…



So let’s run my LoadBalancer IP and see it is working or not…

Yo… It’s working :)
GitHub Link:
Please clap, if you found it useful…❣
Connect me on LinkedIn for any suggestion or query…You can find some interesting articles there too…👍
Happy Learning :)