Deploy A Load Balancer And Multiple Web Servers On Aws Instance Through Ansible

What are Load Balancer?🤔

What is HAproxy?🤔

What is Ansible?🤔

  • Ansible should be installed in the system.
  • Boto3 library should be installed in the system to work with aws through ansible.
  • Should have an AWS account.
  • Should have basic knowledge of AWS.

Now, we can start

- hosts: all
vars_files:
- secret.yml
tasks:
- name: "LoadBalancer"
ec2:
key_name: "Abhinav"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 1
instance_tags:
Name: LoadBalancer
vpc_subnet_id: "subnet-c3feb38f"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-0776e54adea8e690e"
aws_access_key: "{{ access_Key }}"
aws_secret_key: "{{ secret_Key }}"
- name: "WebServer"
ec2:
key_name: "Abhinav"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: yes
count: 3
instance_tags:
Name: WebServer
vpc_subnet_id: "subnet-c3feb38f"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-0776e54adea8e690e"
aws_access_key: "{{access_Key}}"
aws_secret_key: "{{secret_Key}}"
ansible-vault create --vault-id Abhi@prompt AWS_Instance.yml
ansible-playbook --vault-id Abhi@prompt <playbook name>
  • Create a directory with any name.
  • Here we are working on AWS Ec2 Instance therefore we will be downloading python scripts for AWS created by the Ansible team.
  • ec2.py
  • ec2.ini
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.iniwget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
ansible all --list-hosts
ansible all -m ping
ansible-galaxy init webserver
ansible-galaxy init loadbalancer
- name: install httpd
package:
name: "httpd"
state: present
- name: copy content
copy:
content: "hi from {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: sevice restart
service:
name: "httpd"
state: started
  • In haproxy.cfg file in line number 68 the port no should be “8080”.
  • In line 88, we need to mention our backend servers with IP address.
- name: install httpd
package:
name: "httpd"
state: present
- name: copy content
copy:
content: "hi from {{ ansible_hostname }}"
dest: /var/www/html/index.html
- name: sevice restart
service:
name: "httpd"
state: started
- name: haproxy restart
service:
name: "haproxy"
state: restarted
- hosts: Webserver
become: yes
roles:
- role: webserver
- hosts: Loadbalancer
become: yes
roles:
- role: Loadbalancer
ansible-playbook <file_name.yml>

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

API Management (Endpoint-OpenAPI) GCP & GKE

Added hotkeys and “exclude pairs” support to online pairwise tool

Business Review: The Gateway to Automation

CS371p Fall 2021: Mark Joseph Kuebel

From Player to Pawn? One Engineer’s Perspective (Part 2)

6 month training (full stack java) blog

Setup Jenkins using Docker

Join IOEN on Discord!

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

Abhinav shukla

More from Medium

Top Python Mobile App Development Trends in 2021

Connectivity Options for AWS VPC (Part-1)

How To Deploy WordPress on Kubernetes Cluster (AWS)