Configuring web server and launching load balancer by haproxy using ansible playbook on aws.

Statement :- Deploy a Load Balancer and Multiple Web Servers on AWS instances By ANSIBLE.

♦️ Provision EC2 instances through ansible.

♦️ Retrieve the IP Address of instances using the dynamic inventory concept.

♦️ Configure the web servers through the ansible role.

♦️ Configure the load balancer through the ansible role.

♦️ The target nodes of the load balancer should auto-update as per the status of web servers.

Amazon Web Services:

Amazon Web Services (AWS) 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. These cloud computing web services provide a variety of basic abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

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. It includes its own declarative language to describe system configuration. Ansible was written by Michael DeHaan and acquired by Red Hat in 2015.

Steps for provisioning of EC2 instance through ansible :

  1. Installing boto/boto3 :
    Ansible depends on the Python module boto3 to communicate with AWS API. So, boto3 needs to be installed on your machine.

2. Create ec2 instance on aws from control node.We have to write ec2.yml file as below :

Storing your keys in Ansible vault :

Once open, add the following to it:

Once you save the file, all the content will be encrypted. You can verify it as:

3. Now run ec2.yml file to launch ec2 instance.

Now we Can see that Instance On AWS Has Been Launched

4. Now For Proceeding we need IP Of Our Instance Launched on aws . so we can just see and type the ip here in ansible inventory file but i am using dynamic inventory concept using this concept we can get the ip of launched instance on aws using one python program that u can get from ansible github.

Steps to retrieve the IP Address of instance using a dynamic inventory concept.

  1. First we have to download python code from github using wget which fetch ip addresses.

now make it executable :

2. For initiating ec2.py file we have to export region, access key, secret key as:

3. Now configure the inventory as :

4. Now checking all hosts by:

5. Now checking the connectivity between controller node and managed node :

Steps for configuring the webserver and load balancer through ansible.

  1. Now For Configuring webserver and load balancer in aws instance we need to do ssh and for this we will require private key.

♦️Transfer your private key to the controller node by using winscp software.
♦️After this you need to make it accessible.

2. As we know dyamic ip first create an inventory containing two groups one for load balancer and other for webserver.

now again checking all hosts :

3. write playbook for webserver configuration and load balancer configuration on ec2 instance as:

Here I am using template module for copying haproxy configuration file and doing some changes in it .

This is the by default haproxy config file. but when we run the playbook this should be replaced my ec2 instances dynamic IP.

4. Run the above playbook as:

Now You can see by one playbook “HAPROXY” load balancer and “apache webserver” has been configured.

15.207.21.207:80 using LoadBalancer IP at port 80 we can access to the webservers and load is also balancing.

Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #ansible #automation#aws #cloud

--

--

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