Deploying Load Balancer and Web Servers through Ansible.
Tags: AWS, Ansible

What is Ansible ?

Ansible is an automation and orchestration tool popular for its simplicity of installation, ease of use in what concerns the connectivity to clients, its lack of agent for ansible clients and the multitude of skills.

Ansible functions by connecting via SSH to the clients, so it doesn’t need a special agent on the client-side, and by pushing modules to the clients. The modules are then executed locally, on the client-side, and the output is pushed back to the Ansible server.

What is Load Balancer?

Load balancing is defined as the methodical and efficient distribution of network or application traffic across multiple servers in a server farm. Each load balancer sits between client devices and backend servers, receiving and then distributing incoming requests to any available server capable of fulfilling them.

Task Description:-

🔅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.

Let’s get started with our Task…

There are a few prerequisites :

Ansible should be installed.

Boto library should be installed.

You should have an AWS account.

Now before starting with the playbook we will create a vault file that will contain credentials like access key and secret key.

ansible-vault create — vault-id nitya@prompt vars.yml

using this command we can secure our credentials as it cannot be accessed without a password.

Now we will create a security group in AWS it can be done via playbook also but here we will create a security group manually.

For this, we will go to our AWS console and there we will search EC2 instance and then Security Group.

Here we will create a new security group by specifying some INBOUND RULES and some OUTBOUND RULES.

Now let’s create a playbook for launching ec2 instances.

ansible-playbook — vault-id nitya@prompt Servers.yml

using the above command we will run our playbook and instances will be launched.

Now, we can see that our Servers have been launched successfully.

After this, our task is to fetch the IP address of the launched instances so that we can work on those instances through ansible.

We can fetch IP address by two methods

Static

Dynamic

In this case, we will use the Dynamic Inventory.

For this, we will be downloading python scripts for AWS created by the Ansible team.

We have to make a few changes to the files downloaded.

In the ec2.py file change to python3.

In the ec2.ini file change your region, access key, secret key.

Then we will export region, access key, secret key.

export AWS_REGION=’ap-south-1'

export AWS_ACCESS_KEY_ID=’ your access key ’

export AWS_SECRET_ACCESS_KEY=’ your secret key ’

Now we will make these files executable by using commands-

chmod +x ec2.py

chmod +x ec2.ini

After this just run

ansible all --list-hosts

Ansible will automatically fetch all the IPs of the instances in the given region.

Now, we will update the IP’s in our inventory file-

Now, let’s check the connectivity by running the command-

ansible all -m ping

So, our ec2 instances are launched and we have connectivity with them.

After this, we will create two roles

one for webserver

one for the load balancer.

ansible-galaxy init webserver

ansible-galaxy init loadbalncer

First, we will configure the webserver. Go inside the tasks folder which is present inside the webserver role.

cd webserver

cd tasks

vim main.yml

Here we will write the playbook for a webserver.

Now we will configure the load balancer.

We have to make few changes to the haproxy.cfg file.

Now we will be writing the playbook for loadbalancer.

Now we will go to the handlers folder and edit the main.yml file.

We also have to tell ansible where our roles are present. So, inside ansible.cfg file we will write

roles_path = path where roles are created

Now we will create our final playbook for running roles.

Let’s run the playbook and see if everything works good.

Lets check if it works.

It’s working fine finally we have created multiple servers and balancing the traffic between them through the load balancer.

Github Link for the code-

https://github.com/Nitya2503/Ansible_Task3.git