This project is about creating a dynamic inventory for load balancing using Ansible. In this project, I have used the AWS EC2 instances as the inventory servers and HAProxy as the load balancer. The project is divided into two parts. The first part is about creating the dynamic inventory hosts and the second part is about dynamically updating the HAProxy configuration file.
In this Project, I utilized Ansible automation to streamline the provisioning and management of cloud-based infrastructure, In my case I used AWS EC2 instances. The primary goal is to automate the deployment of web servers using Apache HTTP Server (httpd) on dynamically provisioned EC2 instances and orchestrate a dynamic load balancer with HAProxy for distributing incoming traffic across these instances.
- Dynamic Inventory: The EC2 instances are dynamically added to the inventory file using the Jinja2 template.
- Load Balancing: The HAProxy configuration file is dynamically updated with the IP addresses of the EC2 instances.
- AWS Account
- IAM User with programmatic access
- Linux OS (I used Amazon Linux 3)
Follow these steps to use this repository:
-
Clone the repository to your local machine:
git clone https://github.com/mathesh-me/ansible-dynamic-inventory-lb
-
Change the directory to the cloned repository:
cd ansible-dynamic-inventory-lb
-
Update the
aws_access_key
andaws_secret_key
in theaws_credentials.yml
file. -
Update the
key.pem
file with your AWS key pair. -
Update the
aws_ec2_configs.yml
file with your EC2 instance configurations. -
Run the
haproxy.yml
playbook to install HAProxy on the localhost:ansible-playbook haproxy.yml
-
Update the haproxy.j2 file as per instructions in the file.
-
Run the
create_instance.yml
playbook to create the EC2 instances:ansible-playbook create_instance.yml
-
Run the
httpd.yml
playbook to install the Apache HTTP Server on the EC2 instances:ansible-playbook httpd.yml
-
Run the
load_balancer.yml
playbook to create the dynamic inventory:ansible-playbook load_balancer.yml
-
Now you can access the load balancer using the public IP address of the load balancer on port 5000.
- Medium
- In this blog post, I have explained How to do this project step by step. If you wnat to develop this project from scratch, you can follow the blog post.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are always welcome! If you have any suggestions or improvements, feel free to create an issue or a pull request.