LoadBalancer & Web Server configuration in AWS using Ansible

Arvind Ramugade
6 min readSep 7, 2020


Ansible is an open-source configuration management tool. Ansible is an automation engine that automates software provisioning, configuration management, and Application deployment.

Ansible has following features & hence its Virally adopted.

1. Simple
2. Powerful
3. Agent-less- Since its agent-less it can be used for Configuration Mgmt, Network Automation, Workflow Orchestration

Ansible can be used to do automation in following areas. It enhances speed, reduces human errors & provides consistency

a) Cloud
b) Containers
c) Networks
d) DevOPS
e) OS
f) Storage
g) Windows

Use Cases of Ansible

Following are the use cases of Ansible

1. Platform Automation — Linux automation including RedHat, Linux, Ubuntu,
— Windows automation
— Cloud automation

2. Network Automation — Ansible has 65% N/W platforms, 1000+ N/W modules, 15_ Galaxy Roles

3. Security Automation — Ansible is not a security solution , it can help security teams “Stitch Together” the numerous security solutions.

4. Use Cases of Ansible in Configuration Management

. System Configuration & Drift management (Drift stands for non-standard)
. Manage configuration of On & Off premise resources using secure protocols such as SSH, API, WinRM (Microsoft Certified secure protocol)
. Patch management, certificate management
. Hypervisor management

Key Components of Ansible

  • Controller Node: system on which ansible software is installed
  • Managed Node : system which is managed by controller node, on which ansible run its command to configure
  • Ansible Modules: Modules are libraries which helps us achieve desired purpose. e.g. package module helps us to install Apache Web Server.
  • Inventory: Inventory is the database, where all the managed node IPs are given, which is used by ansible. The path of the inventory file is saved in ansible.conf file which uses it to identify managed node and to access managed node.
  • Tasks: We can create tasks & reuse it.
  • Variables: We can declare the variables to be called in playbook.
  • Playbooks: Playbooks includes configurations steps using which we can provision, configure & manage application deployment.

Now let’s focus on our task in which we need to set up Load Balancer & Web Severs using Ansible playbook , concept of roles &

Task Description :-

Statement :- Deploy a Load Balancer and Multiple Web Servers on AWS instances using Ansible.

♦️ Provision EC2 instances through ansible.

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

♦️ Configure the web servers using Ansible role.

♦️ Configure the load balancer using Ansible role.

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

Let’s begin by creating ec2 instances (one for Web Server & other for LoadBalancer) on AWS from control node.

We have to write yml file as below :

Ansible code for launching Ec2 instances

Output of the Ansible-playbook is as follows

Ansible Playbook execution for ec2 instances

We can verify the EC2 instances which are launched in AWS as follows :

EC2 instance (Web Server)
EC2 instance (Load Balancer)

We’ll launch 2 more web servers using above Web Server EC2 instance

Web Servers EC2 Instances

Now we have to configure Ansible config file using dynamic inventory concept.

we can create /myhostinventory folder to store Dynamic IPs of EC2 Web Server instances launched on AWS.

After that we need to download following files in above folder as under

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

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

Then we need to export following variables & change permissions of files

Modified inventory will be as under

Ansible Config file for getting IP of Web Server Instances

We can now get the IPs of Instances launched on AWS as follows

Host List

Also, we can check the connectivity

Connectivity checking between CN & Managed Nodes

Next step is to configure Web Server & Load Balancer configuration files.

Ansible Roles

Ansible roles can be configured for the more systematic management of the Ansible playbooks.

We’ll be launching roles as webserver & lbserver and the modify the yml files in task folder of each role as follows :

Web Server configuration Playbook is as below

Web Server configuration Playbook

Load Balancer configuration playbook

LoadBalancer configuration playbook

Inventory File needs to be modified using the IPs which we received in previous step as follows : myhosts.txt

Inventory File

We can copy the pem key file from windows to CN using WinScp.

We need to update Ansible config file using this inventory

Ansible Config file

We need to use template module for copying haproxy configuration file from load balancer templates and do below changes in it.


Now we are almost ready to run the set up workbook as under

set up playbook
Set up playbook execution for Web Server Configuration
Set up playbook execution for Load Balancer Configuration

As can be seen above our LoadBalancer & Web Server have been configured properly.

We can check Load Balancing using the IP of Load Balancer as follows

Each time when we refresh it will redirect to the IP of different Web Server EC2 Instance.

Load Balancer redirect traffic to different EC2 instance every time

We can also login to Load Balancer using putty & verify the haproxy.cfg to ensure that the Web Server IPs are reflected in it.

Connecting to Load Balancer using Putty
haproxy.cfg in Load Balancer

Using Curl commands as well we can verify Load Balancer configuration (each time it routes traffic to diff. IP)

Curl command for Load Balancer verification



Arvind Ramugade

Cloud Professional experienced in BFSI ,Telecom, Insurance domain with fortune 500 clients spread across USA, Europe, Canada, Australia and India.