Make Inventory dynamic and Automate web server and load balancer creation with Ansible Roles.

--

In this story, I am going to set up a Reverse Proxy server with the help of Ansible Roles.

What is a Reverse proxy?

A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

What is Round Robin?

Roundrobin load balancing is one of the simplest methods for distributing client requests across a group of servers. Going down the list of servers in the group, the roundrobin load balancer forwards a client request to each server in turn.

For achieving this setup we are going to use the Haproxy server. So without wasting any time let’s jump on practical.

By default inventory (ip.txt) is empty. It will get configured as we run launch- instances.yml playbook

You might think what I wrote in this playbook so will configure the inventory file dynamically so let’s take look into tasks.

the first task is about to include role. As the name suggests this will include a role in our playbook.

The second task will gather information about ec2 instances with the help of the ec2_instance_info module and we are storing output into a variable named x so that we can take reference of this information in further tasks.

the third task ensures about the information gathered from the second task is stored into a variable.

last but not least this task will change entries from inventory and make them ready for the next playbook or further configuration. This task also contains some jinja2 keywords which will make it dynamic to use.

Now we are ready to go, So let’s run the playbook with command.

ansible-playbook launch_instances.yml --ask-vault-pass

Note: Above command contains an ansible-vault file that is used to store our credentials in an encrypted format. To use it in the current play we have to give it permission by — ask-vault-pass keyword. This will take input and feed to the vault.

The output of this command is shown below.

Now if you check your AWS management console you can see three instances are launched by ansible and they are in a healthy state. Now it’s time to configure these three instances. You can configure it at runtime also but I used a 2-way approach.

The playbook which will configure these instances is shown below.

In this playbooks we are using two plays one for configuring apache webservers along with PHP and the next play will configure the Haproxy load balancer in the first IP. Let’s run this playbook.

Now, this playbook executed fine so If we check quickly load balancer IP in the browser you can see the following result.

Yes… this load balancer is working in a really great manner. So In this way, you can automate this process.

Conclusion:

Today is the world of the Internet and all the content is hosted on the webserver and millions of client use the services and to serve this huge number of client company require many webservers running but the challenge is all the webserver have a different IP address so the client has to remember all the IP which is practically not possible so to overcome this challenge we use Load Balancer and to automate this process we used Ansible role.

checkout my GitHub for code.

Thanks for reading… If any questions please feel free to leave a comment below and Do connect with me on these platforms.

  1. Mail: yashpanchwatkar@gmail.com
  2. LinkedIn: https://www.linkedin.com/in/yash-panchwatkar/

Stay tuned for such awesome stories!

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--