Configuring HAProxy on AWS using ansible.

We are going to see today that how we can provision three ec2 instance on AWS and configure HAProxy on it in singe click using ansible.


  1. and ec2.ini files are mandatory as these files are the one which help us in implementing the dynamic inventory concept. click here to download the files.
  2. Boto3 library for python. As without boto3 will not able to go to AWS. In simple boto3 is the one which has the capacity to AWS and retrieve the data.
  3. AWS IAM user is also mandatory. Create IAM user and give the user programmatic access. After you create IAM user you will get AWS secret access key ans AWS excess id. We need to provide it to the program, then only program will able to reach the targeted AWS account. We can think it as login and password.
  4. We also need the configuration file of HA Proxy in controller node and do some changes in it so that we can update it in load balancer instances. Below is the picture code HA proxy configuration file copy which I’ll be updating in load balancer . We need to update the highlighted part of configuration file. I have used for loop so that if any other OS in future comes in the LoadBalancer(lb) group so we don’t need to come and update the config file here. While we run the play book it will automatically detect the new OS and update it.

How I implemented all these setup?

  1. I created two yml file one for provisioning the ec2 instances and other for configuring loadbalancer. Click here for the GitHub link. Below I am also provide Images of the code.

Running the playbook is very easy. The challenge is how to achieve all these in a single execution. As we know if we run playbook for provisioning the ec2 instances, the instances will be provisioned but if we want to do something on those provisioned instances, using same playbook it will not work. As when we run playbook ansible 1st looks in the inventory and get the details and then does the tasks written in playbook, so after instances is provisioned then it gets updated in inventory and ansible has still that old inventory data. So for achieving all this in a single click I did a trick with the help of shell module I run a playbook inside the playbook I wrote for provisioning the instances. Now ansible will look at new inventory as I’m running a playbook with the help of shell command and we can do whatever we want to perform on new instances. So I run Load balancer configuration playbook inside instances provisioning playbook and got the desired output in a single click.

You can see the demo video here:

We can also achieve it using Ansible-roles. Concept is same, but there is one difference between Roles and playbook. Role is a folder which contains different folder for tasks, variables etc.

In our demo we don’t need variable as we haven’t used and variable. But we have to perform the tasks so we need tasks folder inside our roles inside which there should be a yml file which will performs the tasks. So I’m adding the picture of main yml file which will call the roles to run:

Below is the picture of tasks each role will perform os provisioning:

Below is the picture of tasks each role will perform the task for webserver:

Below is the picture of tasks each role will perform the task for Loadbalancer:

Thank You!!