HAPROXY-HTTPD ON AWS WITH DYNAMIC INVENTORY BY ANSIBLE

darshil shah
4 min readApr 29, 2021

TASK 12.2 :

* launch ec2 instance on aws using ansible playbook

* configure httpd webserver and then on the top of that configure haproxy load balancer using dynamic inventory

Ansible :-

Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows.

Haproxy:-

HAProxy is free, open source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient

HTTPD:-

The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation

AWS:-

Amazon Web Services is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis

As per given task we have to launch EC2 instance through ansible playbook , so for that we can use ansible modules to connect with aws but for that we have to download an python library named boto3 !

to install we can use this command :

$ pip install boto3

before launching the web server we need to understand the use-case of our task :

when we launch instance and configure it as web-server and then after we have to setup the ip’s in our haproxy load-balancer configuration file . but , everytime we launch the ec2 instance aws provides the public ip to it so fot that we have to use an concept called dynamic inventory so that we can able to fetch the ip’s of webservers and put it on the config file of haproxy on the run time dynamically !

to achive dynamic inventory concept we are using two python files precreated which helps us to fetch the information about our launched instance so that we can use information as the targetted node dynamically !

https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.py

https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.ini

Both files need to be in executable format:

chmod +x ec2.py
chmod +x ec2.ini

After that we have to set some global variables because the ec2.ini file needs some global variables to connect to the AWS and in our case it needs credentials as global variable !

$export aws_access_key_id = <your_access_key_here>
$export aws_secret_access_key = <your_secret_key_here>

After that we have to edit our ansible inventorty file as shown and we have to put our aws secret key which will be later on used to connect to the instance while configuring our instance as load-balancer and web server !

as shown we are using ec2.py as an inventory and a remote user we are using ec2-user because aws provides this user as a default user then we will use sudo and become method root with method called privilege esclation !

Now lets run the instance playbook file which will launch instance and the dynamic inventory will fetch the ip’s

using this playbook ansible will launch total 3 instance with tag name web_server and loadblancer !

now we will run this file and it will use both of the roles which is shared on my github which will dynamically update the ip in inventory file using the files we have used !

GITHUB URL:-

https://github.com/sdarshil/HAPROXY_HTTPD_AWS_DYNAMIC/

Hurray ! , we have successfully set-up loadbalancer with dynamic inventory file on the top of aws using ansible !

--

--

darshil shah

Integration is key , and thats what i love to do !