Configure HAProxy server on EC2 Instance with help of dynamic inventory in Ansible

Girija Varma
4 min readApr 23, 2021

In this article, we are going to use the concept of dynamic inventory and how we can configure the HAProxy server on ec2 instances in AWS.

Ansible is a simple open-source automation platform that provides a complete Infrastructure as Code means that you can use a machine-readable automation language and describe your application. It is an automation engine that runs the Ansible Playbooks. It is simple, agentless and secure.

HAProxy is a free, open-source platform that provides a high availability load balancer and proxy server for TCP(Layer 4) and HTTP(Layer 7) based applications that request across multiple servers.

First install ansible:

# pip3 install ansible 
command to check ansible version → ansible — version
ansible.cfg file

Here I have used roles to configure and manage everything

Steps for Dynamic Inventory:

  1. Install boto and boto3 package of python:
# pip3 install boto
# pip3 install boto3
Install the boto and boto3 package

2. Download the ec2.py and ec2.ini file from the given URL:

https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py

https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Download ec2.py and ec2.ini

3. Make ec2.py and ec2.ini files executable:

Create a folder (hosts) and move these files here, and before executing these files, in the ec2.py file, in the first line instead of python write python3.

ec2.py

Now make the file executable with the command

chmod +x ec2.py
chmod +x ec2.ini
executable files

4. Export the credentials into your system:

# export AWS_REGION=’ap-south-1'
# export AWS_ACCESS_KEY_ID=’Your_AWS_Access_Key_ID’
# export AWS_SECRET_ACCESS_KEY=’Your_AWS_Secret_Access_Key’

You can get the Access ID and Secret Key from AWS IAM.

5. To perform actions on ec2 instance we have to provide the Key also. For that put the key from windows to linux system by Winscp.

change the format of key:# chmod 600 <your_keyname.pem> 

As the key is in ansible.config file so you have to provide the key in given location same as ansible.cfg file.

6. Now to check details of running instances:

# ./ec2.py --list 
# ./ec2.py

Ansible Roles

Ansible roles provide a way to reuse Ansible code generically. You can do all tasks, variables, templates, files, and other resources to provision the infrastructure or applications.

To create role

# ansible-galaxy init <your_rolename> 

To check all the roles:

# ansible-galaxy list 

Here I have created 3 roles to manage this task:

  1. loadbalancer: It will configure one instance as haproxy server that acts as loadbalancer
  2. mywebapplaunch: It will configure instances as webserver
  3. awsprovision: It will launch ec2 instances

To play these roles, I have created other playbook call lbalancer.yml

lbalancer.yml

Here in the above file,

vars_prompt: It will ask for number of instances
- meta: refresh_inventory :- refresh the inventory from where dynamic hosts will be created.

From these hosts, it automatically pick the hosts as tag_Name_lb and tag_Name_myweb.

tag_Name_lb is configured as a loadbalancer and tag_Name_myweb is configured as webservers.

⭐️For the complete code use this GitHub repository

The output of playbook,

lbalancer.yml

After successfully working of playbook you can see the running instances n your AWS account.

running instances

Thank you.

--

--