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

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..

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

👉 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 …

👉 Here is the variables.yml file

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

👉 You can check in AWS Console itself …

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 …

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

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

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 ~

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

Step 5:

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

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

Step 6:

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

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 …

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

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 …. 👋

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