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

Nithish Kumar
5 min readOct 5, 2020

--

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
vars:
vmtags:
— "wb1"
— "wb2"
— "wb3"
— "LB"
tasks:
— name: Launching four instances on AWS Cloud
ec2:
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 }}"
instance_tags:
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 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
//make both files executable# chmod +x ec2.py
# chmod +x ec2.ini
// Export your AWS credentials and AWS Region ...# export AWS_REGION='ap-south-1'
# export AWS_ACCESS_KEY_ID='XXXXXXXXXXXXXXX'
# export AWS_SECRET_ACCESS_KEY='XXXXXXXXXXXXXXXX'

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

 # ./ec2.py

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 ~

[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
[privilege_escalation]
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
package:
name: "haproxy"
state: present
- name: Copy haproxy conf to target IP
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
notify: restart haproxy
- name: Start HAPROXY Service
service:
name: "haproxy"
state: started
//write the below code in loadbalancer/handlers/main.yml
---
# handlers file for loadbalancer
- name: restart haproxy
service:
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
package:
name: "httpd"
state: present
- name: copy required webpages to target IP’s
copy:
content: "Hello, This is {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: Start the service
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
roles:
— loadbalancer
- hosts: webservers
roles:
— 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 github link for code …

👋 …. Signing Off …. 👋

--

--

Nithish Kumar
Nithish Kumar

Written by Nithish Kumar

Aspiring DevOps/Cloud Engineer. #Believe in you.

No responses yet