Integration of Ansible with Reverse Proxy
Hello,
In this Blog, I tried to show how can use an Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it's configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.
Ansible considers what is declared in it's configuration file.
Ansible Configuration file must be in /etc/ansible folder.
Name of Configuration file can be anything but extension must be .cfg
Like in Ansible Configuration File,
- /root/target.txt is mentioned as inventory file so /root/target.txt will be considered as inventory.
- host_key_checking=false , Because in Automation, user don't get prompt for entering yes/no for key check while connection which causes error and so removed this asking part of yes/no for key check by host_key_checking=false and so, error-free Automation happens).
In target.txt,
- Declared a host group Frontend consisting of IP,username,password for the node upon which we want to setup as Reverse Proxy.
- Declared a host group Backend consisting of IP,username,password for the nodes whom to act as web-servers.
Note: Name of host groups can be anything, as per desire.
Let's create Ansible Playbook for same
- Configuring yum repository on all managed nodes so that if any software installation requires, can happen easily.
On Backend host group,
- Stop firewall so that, we can ping managed nodes, in case we need in future.
- Configuring managed nodes as Webserver .
for this;
- Installing httpd software(software required for Apache webserver).
- Copy webpages(here, page.html) to Document Root(here,/var/www/html).
- Starting web services on Managed Nodes.
On Frontend host group,
- Stopping firewall so that can connect to any service running on any port .
- Configuring managed node as Reverse Proxy.
for this;
- Install software for Reverse Proxy, here installing haproxy software for Reverse Haproxy.
- Register Backend Nodes in configuration file of Load Balancer (here, Configuration file for Haproxy software is haproxy.cfg) .
BUT....
Here, instead of seperately registering Backend Nodes,
Registering of Backend Nodes is happening Automatically!
For registering Nodes into Load Balancer,
>> To retrieve Backend Nodes IP automatically, using pre-defined Ansible variable, groups.
Important Note:
groups variable has value in key-value format in which host groups names are key names like here key names will be host group names: Frontend, Backend, all and values for these keys will be IPs present in respective host group names.
>> To register multiple nodes or entering multiple entries, using for loop for this repetitive task.
>> Using concept of groups variable and for loop, retrieve IPs present in Backend host group( group of managed nodes whom we want to configure as webservers) one by one and register entry for each managed node (whom to configure as webservers) in configuration file of load balancer so that these managed node (who are configured as webserver) can managed by Load Balancer.
3. Starting reverse proxy service.
Let's Run this Ansible Playbook
After running plays of Backend host group, webservice started and wabpage is deployed.
By default, apache webserver starts service on port number 80 and deployed webpage is page.html.
We can see webages get deployed and webservice started on port 80 in Backend Nodes.
After running plays of Backend host group, Managed Nodes present in Backend host group get registered in load Balancer in Configuration file of Load Balancer.
By default, haproxy service runs on port number 5000.
We can see webpage(page.html) that is deployed on Backend Nodes,using Load Balancer.
Webpage present on different Backend Nodes only, but can be seen using common port of Load Balancer.
Ansible Playbook and webpage Availabe here!!
THANK YOU for giving it a read. Hope it is helpful!!