Configure Web server and Load balancer using Ansible on ec2 instance

Dhiraj Kumar
3 min readJan 13, 2021

Web server: A computer which gives service to host the website for real world is known as web server , i have configured apache software on OS for use as web server

Apache web server is run on port number 80 , it can be changed . Root directory of apache webserver is “/var/www/html”

Load Balancer: Load balancer balance the traffic of webservers or applications . It transfer the traffic among servers so that single point of failure is avoided.

Haproxy is free open source software that provides a high availability load balancer and proxy server for tcp and http based application , that spreads requests across multiple servers.

Ec2: In Aws many services are available, for computing power EC2 service is used . Using EC2 service instance is launched which have OS installed, which is installed using AMI(amazon machine image).

For give port number and root directory according to use case there is one file have to create , i have created file “/etc/httpd/conf.d/conf/dhiru.conf” as:

For configure load balancer dynamically and update the backend server in haproxy cofiguration file use jinja technique and create file as:

For provisioning ec2 instances and configured load balancer and backend web server ansible playbook is created

Ansible Playbook:

- hosts: localhost
tasks:
- ec2:
region: us-east-1
image: ami-0be2609ba883822ec
instance_type: t2.micro
vpc_subnet_id: subnet-b64fced0
group_id: sg-d51499e1
key_name: N.verginea_key_pair
instance_tags:
Name: "WebServer"
count_tag:
Name: "WebServer"
exact_count: 2
aws_access_key: '******************'
aws_secret_key: '***********************'
register: ec2
- hosts: localhost
tasks:
- ec2:
region: us-east-1
image: ami-0be2609ba883822ec
instance_type: t2.micro
vpc_subnet_id: subnet-b64fced0
group_id: sg-d51499e1
key_name: N.verginea_key_pair
instance_tags:
Name: "LoadBalancer"
count_tag:
Name: "LoadBalancer"
exact_count: 1
aws_access_key: '*******************'
aws_secret_key: '**************************************'
register: ec2
- hosts: tag_Name_WebServer
tasks:
- name: "install apache "
package:
name: "httpd"
state: present
- name: "install php"
package:
name: "php"
state: present
- copy:
src: "index.php"
dest: "/var/www/html"
- template:
src: "dhiru.conf"
dest: "/etc/httpd/conf.d/conf/dhiru.conf"
- service:
name: "httpd"
state: restarted
enabled: yes
- hosts: tag_Name_LoadBalancer
tasks:
- name: "install haproxy"
package:
name: "haproxy"
state: present
- template:
src: "haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"
- service:
name: "haproxy"
state: restarted

In this ansible playbook ec2 module is used to launch the ec2 instances with instance tag which will give idempotance nature to ec2 module

After launching ec2 instances instance tag is used as host group for run task .

For dynamically update inventory file pre-created programme is used , which is available on github. Go to below link

https://raw.githubusercontent.com/vimallinuxworld13/ansible_dynamic_inventory/master/hosts.py

Run below command to run ansible-playbook to complete all tasks

ansible-playbook lb_web_config_ec2.yml

After all the above steps use url as “https://public_ip_of_loadbalancer:5000” This url is the end point of load balancer instance which have two backend web server .

--

--