LoadBalancer & Web Server configuration in AWS using Ansible


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 :

Output of the Ansible-playbook is as follows

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

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

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

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

Modified inventory will be as under

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

Also, we can check the connectivity

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

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

Load Balancer configuration playbook

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

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

We need to update Ansible config file using this inventory

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

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.

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