Task overview

Pravat kumar Nath sharma
5 min readSep 28, 2021
  • 12.1 Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it’s configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.
  • 12.2 Configure the same setup over AWS using instance over there.

Prerequisites

  • I used Redhat-8 as a controller node. Make sure your controller node have proper internet connectivity .
  • Red Hat 8 machine with Ansible installed and configured to connect to your Ansible hosts using SSH keys. Make sure the control node has a regular user with sudo permissions and a firewall enabled.

First configure the ansible dynamic inventory so that we can fetch IPs dynamically. and then launch all the operating system over cloud then third and setup load balancer through Haproxy. and my all the files and folder related to this task is in /ansible/arthtask12.2/ folder.

DYNAMIC INVENTORY SETUP

Here you will learn how to set up a dynamic inventory on AWS using boto3 and boto , ec2.yml and ec2.ini file.

Follow the steps carefully for the setup.

Step 1:

  • Install python3 “$ yum install python3 -y”
  • Install the boto3 and boto library “$ pip3 install boto3” , “$ pip3 install boto”

step2:

  • create a directory “$ mkdir /Ansible/ws3”. In my case i created ws3 but you can create anywhere you want. Only we have to tell ansible by writing in their configuration file where is those file where is ec2.py module.
  • download ec2.yml and ec2.ini from ansible official dynamic inventory GitHub link in /ansible/ws3 folder. both the files should be in same folder.
[Download ec2.py inventory ]
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.py
[Download ec2.ini inventory ]
wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

Now make your ec2.py executable. For this we need to use one command

chmod +x ec2.py
  • open ec2.py file and change env python to python3 in the first line because this python code is written in python2 but if in your case you can use if you are already installed with python2 interpreter.

In the next step, we have to just set environmental variable to your aws_access_key_id and secret_access_key . for this we can use export command.

export  AWS_REGION='ap-south-1'export AWS_ACCESS_KEY_ID='LDOIGEKDXXXXXXXXX'export AWS_SECRET_ACCESS_KEY='sdkoierjkdjoidfjkgdofixxxxxxxxxxxxxxxxxxxxxxxxx'

Note: After set this environmental variable just go to /root/.bashrc file and update this entry because every reboot the VM, this environmental variable lost automatically.

Now update this entry /root/Ansible/ws3 directory in ansible configuration file(/etc/ansible/ansible.cfg) and also set aws private_key and user through which you wanted to launch os on aws.

Note: — To setup the haproxy LoadBalancer on top of aws, we need key to login to the instance so make sure your key should be available in your localhost otherwise it will fail to SSH. In my case i copied my key at document root /root/cloud-key.pem and also update this entry in Ansible configuration file in the above.

Remember:- I didn’t update IP of instance in any of the config or ip.txt file because ansible creates automatically dynamic inventory so we don’t need to do this.

LAUNCHING INSTANCE ON TOP OF AWS USING ANSIBLE PLAYBOOK

Ansible playbook to launch 3 instance on top of aws cloud.

- hosts: localhost
vars_files:
secret.yml
tasks:
- name: "Launching Proxy Server"
ec2:
region: ap-south-1
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
vpc_subnet_id: subnet-0288bbf00ed3128d7
count: 1
state: present
instance_type: t2.micro
key_name: cloud-key
assign_public_ip: yes
group_id: sg-0612a79a1fdb041ff
image: ami-08f63db601b82ff5f
instance_tags:
name: Haproxy


- name: "Launching WebServer"
ec2:
region: ap-south-1
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
vpc_subnet_id: subnet-0288bbf00ed3128d7
count: 2
state: present
instance_type: t2.micro
key_name: cloud-key
assign_public_ip: yes
group_id: sg-0612a79a1fdb041ff
image: ami-08f63db601b82ff5f
instance_tags:
name: WebServer

In my case I used external file named as secret.yml to attach aws_access_key and aws_secret_key because I can’t show this publicly.

vim   /root/secret.yml      aws_access:  xxxxxxxx
aws_secret: xxxxxxxxxxxxxxxxxxxxxx
region: ap-south-1

Output of this code

Note:- Here giving instance-tags which is very important , we will use it dynamic configure the haproxy file otherwise it failed.

now try to ping using “$ ansible all -m ping “ to check via command line

Here is some warning but just ignore it and see the connectivity to aws cloud.

we can list all the details about all the instance using “$ ./ec2.py “.

Note this tag_names because it help us to configure the web server setup and Haproxy setup.

CONFIGRATION REVERSE PROXY (WITH APACHE WEBSERVER)

Ansible-playbook to configure the apache setup and webserver setup

- hosts: tag_Name_WebServer 
vars:
web_port: 80
tasks:
- name: Installing httpd software
package:
name: httpd
state: present

- name: Installing PHP interpreter
package:
name: php
state: present

- name: Copying content of Web server
copy:
content: '<pre>
<?php
print`/usr/sbin/ifconfig eth0`;
?>
</pre>'
dest: /var/www/html/index.php
notify:
- Restart httpd

- name: Starting httpd service
service:
name: httpd
state: started
handlers:
- name: Restart httpd
service:
name: httpd
state: restarted

- hosts: tag_Name_Haproxy
vars:
- haproxy_port: 8080
- web_port: 80
tasks:
- name: Installing haproxy
package:
name: haproxy
state: present

- name: Configure the haproxy.cfg file
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
notify:
- Restart haproxy

- name: Starting Haproxy service
service:
name: haproxy
state: started
handlers:
- name: Restart haproxy
service:
name: haproxy
state: restarted

And this is haproxy.cfg file which I wanted to configure with apache……..Update this entry with instance_tag_name otherwise it won’t work.

Now everything is configure properly so just running the ansible-playbook to setup the Haproxy and Webserver on top aws cloud.

everything is working good. now we can check load balancer is working or not.

Final output

we have configure haproxy in <tag_name_loadbalancer> and this contain load balancer IP which is 3.7.68.240

Now browse http://3.7.68.240:8080/ multiple time .

I have written PHP code to print the IP address of that system . if will browse multiple time http:3.7.68.240//:8080/ it will give every time different IP , its means load balancer is working good.

--

--