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

🔅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
— credentials.yml
— variables.yml
— "wb1"
— "wb2"
— "wb3"
— "LB"
— name: Launching four instances on AWS Cloud
key_name: "{{ aws_key_pair }}"
instance_type: "{{ aws_instance_type }}"
image: "{{ aws_image }}"
wait: yes
vpc_subnet_id: "{{ aws_subnet_id }}"
assign_public_ip: yes
region: "{{ aws_region }}"
state: present
group: "{{ aws_SG }}"
aws_access_key: "{{ awsuser }}"
aws_secret_key: "{{ awspass }}"
Name: "{{ item }}"
loop: "{{ vmtags }}"

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

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

# yum install wget -y

# wget

# wget
//make both files executable# chmod +x
# chmod +x ec2.ini
// Export your AWS credentials and AWS Region ...# export AWS_REGION='ap-south-1'

Now run the 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 ~

inventory = /myinventory/ec2ip.txt
host_key_checking = False
remote_user = ec2-user
ask_pass = False
become = True
become_user = root
become_ask_pass = false
become_method = sudo

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

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
- name: Install HAPROXY package
name: "haproxy"
state: present
- name: Copy haproxy conf to target IP
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
notify: restart haproxy
- name: Start HAPROXY Service
name: "haproxy"
state: started
//write the below code in loadbalancer/handlers/main.yml
# handlers file for loadbalancer
- name: restart haproxy
name: "haproxy"
state: restarted

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 …

// write below code in webserver/tasks/main.yml
# tasks file for webserver
- name: Install HTTPD package
name: "httpd"
state: present
- name: copy required webpages to target IP’s
content: "Hello, This is {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: Start the service
name: "httpd"
state: started

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
— loadbalancer
- hosts: webservers
— webserver
// run the above playbook ...# ansible-playbook deploy.yml

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