Configuring web server and launching load balancer by haproxy using ansible playbook on aws.

5 min readOct 26, 2020


Task Description :-

Statement :- Deploy a Load Balancer and Multiple Web Servers on AWS instances By ANSIBLE.

♦️ Provision EC2 instances through ansible.

♦️ Retrieve the IP Address of instances using the dynamic inventory concept.

♦️ Configure the web servers through the ansible role.

♦️ Configure the load balancer through the ansible role.

♦️ The target nodes of the load balancer should auto-update as per the status of web servers.

Amazon Web Services:

Amazon Web Services (AWS) is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.


Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015.

Steps for provisioning of EC2 instance through ansible :

  1. Installing boto/boto3 :
    Ansible depends on the Python module boto3 to communicate with AWS API. So, boto3 needs to be installed on your machine.
pip install boto boto3

2. Create ec2 instance on aws from control node.We have to write ec2.yml file as below :

Storing your keys in Ansible vault :

ansible-vault create awspass.yml

Once open, add the following to it:

myuser: *************************
mypass: **********************************

Once you save the file, all the content will be encrypted. You can verify it as:

3. Now run ec2.yml file to launch ec2 instance.

Now we Can see that Instance On AWS Has Been Launched

4. Now For Proceeding we need IP Of Our Instance Launched on aws . so we can just see and type the ip here in ansible inventory file but i am using dynamic inventory concept using this concept we can get the ip of launched instance on aws using one python program that u can get from ansible github.

Steps to retrieve the IP Address of instance using a dynamic inventory concept.

  1. First we have to download python code from github using wget which fetch ip addresses.

now make it executable :

chmod +x

2. For initiating file we have to export region, access key, secret key as:

export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY_ID="aws_access_key"
export AWS_SECRET_ACCESS_KEY="aws_secret_key"

3. Now configure the inventory as :

4. Now checking all hosts by:

5. Now checking the connectivity between controller node and managed node :

Steps for configuring the webserver and load balancer through ansible.

  1. Now For Configuring webserver and load balancer in aws instance we need to do ssh and for this we will require private key.

♦️Transfer your private key to the controller node by using winscp software.
♦️After this you need to make it accessible.

chmod 600 task2.pem
cat task2.pem

2. As we know dyamic ip first create an inventory containing two groups one for load balancer and other for webserver.

now again checking all hosts :

3. write playbook for webserver configuration and load balancer configuration on ec2 instance as:

Here I am using template module for copying haproxy configuration file and doing some changes in it .

This is the by default haproxy config file. but when we run the playbook this should be replaced my ec2 instances dynamic IP.

4. Run the above playbook as: using LoadBalancer IP at port 80 we can access to the webservers and load is also balancing.

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #ansible #automation#aws #cloud