Ansible Roles to configure LoadBalancer and Apache Web Server !!
Sheetal Agarwal
AWS Community Builder | 4X GCP Certified | 3X AWS Certified | 1X Azure Certified | DevOps Engineer | Cloud Engineer | SRE | Kubernetes | Jenkins | Terraform | Ansible
?? Task Description:
??Create an ansible role myapache to configure Httpd WebServer.
??Create another ansible role myloadbalancer to configure HAProxy LB.
??We need to combine both of these roles controlling webserver versions and solving challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.
What is Ansible:
Ansible is a software tool that provides simple but powerful automation for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis. Ansible doesn’t depend on agent software and has no additional security infrastructure, so it’s easy to deploy.
Ansible Roles:
Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users. The entire hierarchy can be created just by one single command.
Let’s start the practical part -
Here, I have launched 4 ec2 instances on AWS. One for Ansible Controller Node, one for Load Balancer and other 3 for Web Server configuration.
In Ansible Controller Node install the Ansible by using following command.
pip3 install ansible
Check the version of Ansible.
ansible --version
Now update the configuration file of Ansible.
vim /etc/ansible/ansible.cfg
Ansible Inventory file -
Now check the connectivity with managed nodes.
ansible all --list-hosts ansible all -m ping
Now create two Ansible roles, one for WebServer and second for LoadBalancer.
ansible-galaxy init "rolename"
Now, we can see that both the roles have been initialized.
Now after creating the roles go into the webserver roles and create task to configure Web Server.
vim webserver/tasks/main.yml
Now create a webpage to deploy on webserver.
vim index.html
Now define the variables used in main.yml file of webserver roles in vars directory.
vim webserver/vars/main.yml
Now configure the handlers in handlers directory.
vim webserver/handlers/main.yml
Now go into the loadbalancer roles and create task to configure loadbalancer.
vim loadbalancer/tasks/main.yml
Now define the variables used in main.yml file of loadbalancer role in vars directory.
vim loadbanancer/vars/main.yml
Now configure the handlers in handlers directory.
vim loadbalancer/handlers/main.yml
Now update the configuration file of haproxy which will be copied in slave node of loadbalancer.
vim loadbanancer/templates/haproxy.cfg
Now create one playbook which will combine these two roles togather and can run all these task together .
vim task.yml
Here, we can see the entire hierarchy of roles.
Now run the playbook.
ansible-playbook task.yml
We have successfully configured the LoadBalancer
Now type the public_IP of loadbalancer with the HaProxy port number.
Here we can see that IP is switching which means LoadBalancer is configured successfully.
Now add one more Web Server in the Ansible Inventory file and haproxy server help us to manage the outside traffic.
Now check the connectivity with managed nodes.
Now run the playbook.
Now type the public_IP of loadbalancer with the HaProxy port number.
Here we can see that IP is switching between 3 backend servers which means LoadBalancer is configured successfully.
Thank You for Reading the Article !!