Configure LB(haproxy) using Ansible-Roles on AWS.

Prerequisite:

Step-1)configure ansible.cfg for roles,dynamic inventory.

Configure Roles

mkdir /root/ansible_roles.

Configure Dynamic Inventory

mkdir /mydb
https://github.com/ansible/ansible/tree/stable-2.9/contrib/inventory

Configure key for AWS ec2-instance

chmod 600 Key_Name.pem
chmod +x ec2.py

Step-2)Launch ec2 instance using ansible

$pip3 install boto$pip3 install boto3
$ansible-galaxy init ec2
ec2 role
$ansible-vault create aws.yml

Code:

---
# tasks file for ec2
- name: launchdos
ec2:
key_name: "ansbiletask2"
instance_type: "{{ aws_instance_type }}"
image: "{{ ami_id }}"
wait: yes
count: 1
instance_tags:
Name: "web"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
region: "{{ aws_region }}"
state: present
group_id: "{{ security_group_id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
- name: launchdos
ec2:
key_name: "ansbiletask2"
instance_type: "{{ aws_instance_type }}"
image: "{{ ami_id }}"
wait: yes
count: 1
instance_tags:
Name: "web"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
region: "{{ aws_region }}"
state: present
group_id: "{{ security_group_id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
- name: launchdos
ec2:
key_name: "ansbiletask2"
instance_type: "{{ aws_instance_type }}"
image: "{{ ami_id }}"
wait: yes
count: 1
instance_tags:
Name: "web"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
region: "{{ aws_region }}"
state: present
group_id: "{{ security_group_id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
- name: launchdos
ec2:
key_name: "ansbiletask2"
instance_type: "{{ aws_instance_type }}"
image: "{{ ami_id }}"
wait: yes
count: 1
instance_tags:
Name: "loadbalancer"
vpc_subnet_id: "{{ subnet_id }}"
assign_public_ip: yes
region: "{{ aws_region }}"
state: present
group_id: "{{ security_group_id }}"
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"

code:

---
# vars file for ec2
aws_instance_type: "t2.micro"
ami_id: "ami-0ebc1ac48dfd14136"
aws_instance_tags: "ansbiletask2"
subnet_id: "subnet-1105675d"
aws_region: "ap-south-1"
security_group_id: "sg-0c4f0f47c1079e930"

Code:

- hosts: localhost
vars_files:
- /root/ansible_roles/ec2/vars/aws.yml
roles:
- role: ec2
$ansible-playbook --ask-vault-pass ec2.yml

Output:

3 web, 1 for LB

Step-3)Configure webserver on EC2 instance.

$ansible-galaxy init webserver

Code:

# tasks file for webserver- name: httpdconf
package:
name: "{{ package_name }}"
state: present- name: git install
package:
name: "git"
state: present- name: start httpd
service:
name: "{{ package_name }}"
state: started
enabled: yes- name: git for copy code
git:
repo: "{{ git_repo }}"
clone: yes
dest: "{{ location }}"

Code:

# vars file for webserver
package_name: "httpd"
git_repo: "YOUR_GITHUB_REPO"
location: "/var/www/html"

Step-4)Configure haproxy on EC2 instance

ansible-galaxy init haproxy
task/main.yml

Code:

#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:{{ front }}
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
{% for hosts in groups["tag_Name_web"] %}
server app1 {{ hosts }}:{{ back }} check
{% endfor %}

Code:

---
# vars file for haproxy
front: 8080
back: 80
- hosts: tag_Name_web
roles:
- role: webserver
- hosts: tag_Name_loadbalancer
roles:
- role: haproxy
ansible-playbook filename.yml(httpdha.yml)

Output:

Final Output:

simple website
haproxy service is running
running on port 8080
haproxy.cfg

--

--

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