Deploy a Load Balancer and multiple Web Servers on AWS instances through ANSIBLE !!!

Image for post
Image for post

Description:
🔅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.

🏃 Follow the below steps:
Step 1: Install boto and boto3 libraries..

pip3 install boto
pip3 install boto3

Step 2: Write the ansible code to launch 4 EC2 instances namely web1, web2, web3 and LB… Here is the code ~

- hosts: localhost
vars_files:
— credentials.yml
— variables.yml

👉 In vars_file section, I have given two files for aws credentials and the key pair, subnet ids, and security group etc,…
👉 AWS Credentials are critical for us, So use ansible-vault to secure your credentials … Run the below and then give your credentials …

ansible-vault create — vault-id prod@prompt credentials.yml

👉 Here is the variables.yml file

- aws_key_pair: "hcc81"
- aws_instance_type: "t2.micro"
- aws_image: "ami-052c08d70def0ac62"
- aws_subnet_id: "subnet-1f3c4953"
- aws_region: "ap-south-1"
- aws_SG: "default"

Now run the ansible playbook to launch 4 EC2 instances on AWS CLoud …

ansible-playbook --vault-id prod@prompt <filename.yml>
Image for post
Image for post

👉 You can check in AWS Console itself …

Image for post
Image for post

Step 3: We have to retrieve the EC2 instances IP’s using dynamic inventory to configure haproxy in one instance and httpd webserver in remaining three webserves …

# yum install wget -y

# 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

Now run the ec2.py file as shown below ..

 # ./ec2.py
Image for post
Image for post

You can see all the EC2 IP’s of running instances … Now manually type the IP’s in new txt file …

Image for post
Image for post

Now ping those EC2 IP’s whether they are pinging or not !!! . Before pinging do some changes in ansible.cfg file … Here are the settings ~

[defaults]
inventory = /myinventory/ec2ip.txt
host_key_checking = False
remote_user = ec2-user
ask_pass = False
private_key_file=/root/hcc81.pem
roles_path=/path/to/roles
Image for post
Image for post

Step 4: Now create two roles for configuring webservers in three instances namely web1, wb2, wb3 and haproxy in LB instance

# ansible-galaxy init loadbalancer
# ansible-galaxy init webserver
# ansible-galaxy list
Image for post
Image for post

Step 5:

In loadbalancer role ~
Now write the code to install haproxy and edit the config file in controller node…

//write the below code in loadbalancer/tasks/main.yml
---
# tasks file for loadbalancer

Copy the haproxy.cfg to loadbalancer/templates/haproxy.cfg… And do some changes to dynamically retrieve the IP’s of webservers …

Image for post
Image for post

Step 6:

In webserver role ~
Install httpd server and copy the index.html to controller node … Here is the code …

// write below code in webserver/tasks/main.yml
---
# tasks file for webserver

That’s it… Created a two roles to configure load balancer and webservers for balancing the load of all three webservers …

Create another playbook to run two roles one by one …

- hosts: loadbalancer
roles:
— loadbalancer
Image for post
Image for post
Image for post
Image for post

Now type the LB instance public IP to see the output …

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Summary: One-click four instances launched, three webservers provisioned and one load balancer is configured !!!

🤝 Thanks for reading !!!
🌟 Here is the github link for code …

👋 …. Signing Off …. 👋

Written by

Doing Intern at ARTH - School Of Technologies

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