Ansible Roles to configure LoadBalancer and Apache Web Server !!

Ansible Roles to configure LoadBalancer and Apache Web Server !!

?? 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:

No alt text provided for this image


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:

No alt text provided for this image

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.

No alt text provided for this image

In Ansible Controller Node install the Ansible by using following command.

pip3 install ansible

Check the version of Ansible.

ansible --version
No alt text provided for this image

Now update the configuration file of Ansible.

vim /etc/ansible/ansible.cfg
No alt text provided for this image

Ansible Inventory file -

No alt text provided for this image

Now check the connectivity with managed nodes.

ansible all --list-hosts
ansible all -m ping
No alt text provided for this image

Now create two Ansible roles, one for WebServer and second for LoadBalancer.

ansible-galaxy init "rolename"
No alt text provided for this image

Now, we can see that both the roles have been initialized.

No alt text provided for this image

Now after creating the roles go into the webserver roles and create task to configure Web Server.

vim webserver/tasks/main.yml
No alt text provided for this image

Now create a webpage to deploy on webserver.

vim index.html
No alt text provided for this image

Now define the variables used in main.yml file of webserver roles in vars directory.

vim webserver/vars/main.yml
No alt text provided for this image

Now configure the handlers in handlers directory.

vim webserver/handlers/main.yml
No alt text provided for this image

Now go into the loadbalancer roles and create task to configure loadbalancer.

vim loadbalancer/tasks/main.yml
No alt text provided for this image

Now define the variables used in main.yml file of loadbalancer role in vars directory.

vim loadbanancer/vars/main.yml
No alt text provided for this image

Now configure the handlers in handlers directory.

vim loadbalancer/handlers/main.yml
No alt text provided for this image

Now update the configuration file of haproxy which will be copied in slave node of loadbalancer.

vim loadbanancer/templates/haproxy.cfg
No alt text provided for this image

Now create one playbook which will combine these two roles togather and can run all these task together .

vim task.yml
No alt text provided for this image

Here, we can see the entire hierarchy of roles.

No alt text provided for this image
No alt text provided for this image

Now run the playbook.

ansible-playbook task.yml

No alt text provided for this image
No alt text provided for this image

We have successfully configured the LoadBalancer

Now type the public_IP of loadbalancer with the HaProxy port number.

No alt text provided for this image
No alt text provided for this image

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.

No alt text provided for this image

Now check the connectivity with managed nodes.

No alt text provided for this image
No alt text provided for this image

Now run the playbook.

No alt text provided for this image
No alt text provided for this image

Now type the public_IP of loadbalancer with the HaProxy port number.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Here we can see that IP is switching between 3 backend servers which means LoadBalancer is configured successfully.

Thank You for Reading the Article !!

要查看或添加评论,请登录

Sheetal Agarwal的更多文章

社区洞察

其他会员也浏览了