Configure HAProxy Loadblancer With Dynamic Inventory using Ansible on AWS

Adarsha Dinda
4 min readApr 5, 2021

DYNAMIC INVENTORY SETUP

Follow the steps carefully for the setup.

step 1:

  • Install python3 $ yum install python3 -y”
  • Install the boto3 and boto library using following command :

pip3 install boto3

pip3 install boto

step2:

  • create a directory “ mkdir /root/task12.2/inventory ”. In my case i created here but you can create anywhere you want. Only we have to tell ansible by writing in their configuration file where is those file where is ec2.py module.
  • download ec2.yml and ec2.ini from ansible official dynamic inventory GitHub link in my inventory folder. both the files should be in same folder .

[Download ec2.py inventory ]
wget
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

[Download ec2.ini inventory ]
wget
https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Now make your ec2.py executable. For this we need to use one command.

chmod +x ec2.py

  • open ec2.py file and change env python to python3 in the first line because this python code is written in python2 but if in your case you can use if you are already installed with python2 interpreter.

In the next step, we have to just set environmental variable to your aws_access_key_id and secret_access_key . for this we can use export command.

export AWS_REGION=’ap-south-1'

export AWS_ACCESS_KEY_ID=’XXXXXXXXX’

export AWS_SECRET_ACCESS_KEY=’xxxxxxxxxxxxxxxxxxxxxxxxx’

Note: After set this environmental variable just go to /root/.bashrc file and update this entry because every reboot the VM, this environmental variable lost automatically.

Now update this entry “/root/task12.2/inventory” directory in ansible configuration file(/etc/ansible/ansible.cfg) and also set aws private_key and user through which you wanted to launch os on aws.

Note: — To setup the haproxy LoadBalancer on top of aws, we need key to login to the instance so make sure your key should be available in your localhost otherwise it will fail to SSH. In my case i copied my key and also update this entry in Ansible configuration file in the above.

Remember:-

I didn’t update IP of instance in any of the config or ip.txt file because ansible creates automatically dynamic inventory so we don’t need to do this.

LAUNCHING INSTANCE ON TOP OF AWS USING ANSIBLE PLAYBOOK

Ansible playbook to launch 3 instance on top of aws cloud.

Let’s run the playbook :

In my case I donot use external file named to attach aws_access_key and aws_secret_key . You can attach var file for security reason .

Output of this code :

Note:- Here giving instance-tags which is very important , we will use it dynamic configure the haproxy file otherwise it failed.

we can list all the details about all the instance using “$ ./ec2.py “.

Note this tag_Names help us to configure the web server setup and Haproxy setup .

CONFIGRATION REVERSE PROXY (WITH APACHE WEBSERVER)

Ansible-playbook to configure the apache setup and webserver setup

And this is haproxy.cfg file which I wanted to configure with apache……..Update this entry with instance_tag_name otherwise it won’t work.

Now everything is configure properly so just running the ansible-playbook to setup the Haproxy and Webserver on top aws cloud.

Final outcome

we have configured haproxy in <tag_name_loadbalancer> and this contain load balancer IP which is 15.206.160.217

Now browse http://15.206.160.217:8080/my.php multiple time .

✔️✔️✔️️ I have written a PHP code to print the IP address of that system . if we browse multiple time, it will give every time different IP , its means load balancer is working good.

--

--