Deploy a Load Balancer and multiple Web Servers on AWS instance through Ansible

  1. Provision EC2 instances through ansible.
  2. Retrieve the IP Address of instances using the dynamic inventory concept.
  3. Configure the web servers through the ansible role.
  4. Configure the load balancer through the ansible role.
  5. The target nodes of the load balancer should auto-update as per the status of web servers.

In my case I have create ansible configuration file is in/etc/ansible/ansible.cfg folder location.

To create the dynamic inventory by fetching ec2 instance IP we will have to use and ec2.ini file.

So first we have to download these files from GitHub.

Inside file we will have to change the python version to 3

Also we have to file executable.

#chmod +x

To make an API request to AWS EC2 we have to install bolo library and also export the environmental variable needed for boto.

#pip3 install bolo

#export AWS_DEFAULT_REGION=’Enter the region name’

#export AWS_ACCESS_KEY_ID=’Enter AWS Access key’

#export AWS_SECRET_ACCESS_KEY=’Enter AWS Secret key’

Step 4: Create Ansible Roles for launching the instances, configuring webserver and configuring load balancer.

Launch instances for webserver and load balancer:

#ansible-galaxy init instance

Configure webserver:

#ansible-galaxy init webserver

Configure Load Balancer:

#ansible-galaxy init lbserver

First let see instance role:

  1. tasks/main.yml

2. /vars/main.yml

Here we have encrypted the vars file.

Let see webserver role:

  1. tasks/main.yml

Let see lbserver role:

  1. tasks/main.yml

2. handlers/main.yml

3. templates/haproxy.cfg

Here the frontend is bind at port number 5000 and backend(webserver) is at port number 80. Here roundrobin algorithm for load balancing.

For this I am using task3.yml file.

For this I am using setup.yml file.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻