Using Ansible to configure Load-Balancer on AWS using Dynamic Inventory!
Harshit Dawar
Senior Consultant Development@Infogain | RHCA | CKA | 8x RedHat, 7x Azure, 7x Databricks Certified | Technical Architect | Databricks & Linux Foundation Official Instructor | AWS CB | Terraform & Vault Certified
This article will explain the process of provisioning a HaProxy load-balancer with the 3 webservers on AWS cloud using Ansible.
Steps to achieve our goal:
- Provision 4 ec2-instances using Ansible on AWS.
- Configure 3 of the ec2-instances as a webserver on the AWS & the remaining one to be Load Balancer.
Provisioning the EC2-Instances!
Run the below-given code to provision the ec2-instances using ansible.
- name: Ansible-Playbook to provision AWS EC2-Instance hosts: localhost vars_files: # Name of the file which contains the access & secret key for AWS! - password.yaml tasks: - name: Launching an AWS Instance ec2: # Write the name of your key which has to be used for SSH key_name: MyKeyFinal instance_type: t2.micro image: "ami-08706cb5f68222d09" wait: yes group_id: "sg-0202966a4cb160463" count: 3 vpc_subnet_id: subnet-44e49308 assign_public_ip: yes state: present region: ap-south-1 zone: ap-south-1b aws_access_key: "{{ accessKey }}" aws_secret_key: "{{ secretKey }}" instance_tags: Name: "Webserver" # Registering all the details of the provisioned Instance in a variable to retrieve Public IP register: EC2_Instance - name: Print the Information about EC2 Instance debug: var: EC2_Instance.instances[0].public_ip
Run the below-given code to provision the load balancer on the AWS.
- name: Ansible-Playbook to provision AWS EC2-Instance hosts: localhost vars_files: - password.yaml tasks: - name: Launching an AWS Instance ec2: key_name: MyKeyFinal instance_type: t2.micro image: "ami-08706cb5f68222d09" wait: yes group_id: "sg-0202966a4cb160463" count: 1 vpc_subnet_id: subnet-44e49308 assign_public_ip: yes state: present region: ap-south-1 zone: ap-south-1b aws_access_key: "{{ accessKey }}" aws_secret_key: "{{ secretKey }}" instance_tags: Name: "Load-Balancer" # Registering all the details of the provisioned Instance in a variable to retrieve Public IP register: EC2_Instance - name: Print the Information about EC2 Instance debug: var: EC2_Instance.instances[0].public_ip
Now, all the 4 ec2-instances have been configured, to retrieve the IP of all the instances, run the command "ansible all --list-hosts" on the controller node.
The above-shown image has the public IP of all the instances provisioned using Ansible. Verify the IP of the load-balancer EC2 Instance from the aws console. Your AWS Console will look like:
Step 1 is successfully completed now. Let's process to the final step of configuring the web server and load balancer on the provisioned instances.
Configuring the Load-Balancer & the Webserver!
Create 2 ansible playbooks for executing the ansible webserver role & HaProxy role. The link to both roles is mentioned below.
Download the roles from the link mentioned above, & save them to a folder, after that add that path as a roles path in the inventory.
To Execute the webserver role, create an Ansible playbook using the code given below.
- name: Playbook to Configure the Apache Webserver using the Role! # Add the host name corresponding the group created in the Ansible Inventpry for the webservers. hosts: ec2 roles: - role: Apache-Webserver
To Execute the HaProxy role, create an Ansible playbook using the code given below.
- name: PlayBook to configure the HAProxy Load Balancer Setup # Add the host name corresponding the group created in the Ansible Inventpry for the Load Balancer. hosts: HAProxy-LB roles: - role: HAProxy-Load-Balancer
Note: In the HaProxy Role, add your own configuration HaProxy file(according to your requirement) which will be copied using the ansible role.
The project has been concluded now.
Happy Learning!
Information Technology Specialist at ADFAR Tech Ventures
2 年Harshit, thanks for sharing! We at ADFAR Tech are looking for: Inside Sales | BDM Role. Remote Working. Attractive compensation of fixed and variable. If anyone is interested please apply: Job Post Link https://www.dhirubhai.net/jobs/view/3413684326 Regards, Dalia ADFAR Tech Ventures https://www.adfar.tech/