Ansible playbook to Configure Reverse Proxy for Apache Web Server
A Reverse Proxy is a server that sits in front of web servers and forwards client (e.g. web browser) requests to those web servers.
Reverse proxies are typically implemented to help increase security, performance, and reliability.
Task Description:
Creating an Ansible playbook to Configure Reverse Proxy i.e. HAProxy and update it’s configuration file automatically each time a new Managed node (Configured With Apache Webserver) join the inventory.
HAProxy:
HAProxy is a high-performance, open-source load balancer and reverse proxy for TCP and HTTP applications. Users can make use of HAProxy to improve the performance of websites and applications by distributing their workloads.
Steps to Follow:
~ Create an ansible configuration file .
~ Update Inventory
~ Create a HAProxy Configuration File
~Create a webpage folder and put all php code in it
~ Create an Ansible Playbook
~ Run the Playbook
Ansible Configuration File:- /etc/ansible/ansible.cfg
Update the IP in the inventory file: /root/ip.txt
-> LoadBalancer IP:- 192.168.185.7
-> WebServer IP :- 192.168.185.3 ,192.168.185.4
Check the connectivity using ping
ansible all -m ping
Create a HAProxy Configuration File: -
Make changes in the configuration file so that it can automatically update each time when a new Managed node come
Because we have to balance the load among the available servers, so we use a loop to dynamically retrieve the IP of servers each time a new Managed node (Configured With Apache Webserver) join the inventory.
Create an Ansible Playbook
- loadbalancer.yml
- hosts: lb
vars_prompt:
- name: lb_port
prompt: Enter load balancer port number
private: notasks:
- file:
state: directory
path: "/dvd1"
- mount:
src: "/dev/cdrom"
path: "/dvd1"
state: mounted
fstype: "iso9660"
- yum_repository:
baseurl: "/dvd1/AppStream"
name: "mydvd1"
description: "dvd1 for pacakge"
gpgcheck: no
- yum_repository:
baseurl: "/dvd1/BaseOS"
name: "mydvd2"
description: "dvd2 for package"
gpgcheck: no- firewalld:
port: "{{ lb_port }}/tcp"
permanent: yes
state: enabled
immediate: yes- package:
name: "haproxy"
state: present
- name: "copy file to conf file"
template:
src: "haproxy.cfg"
dest: "/etc/haproxy/haproxy.cfg"
- name: "start httpd service"
service:
name: "haproxy"
state: started
enabled: yes
2. myweb.yml
- hosts: myweb
tasks:
- name: "httpd service"
package:
name: "httpd"
state: present
- name: "php service"
package:
name: "php"
state: present
- copy:
src: "/root/ansible_playbook/HaProxy/webpage/"
dest: "/var/www/html/"- name: "start httpd service"
service:
name: "httpd"
state: restarted
Create index.php page
Run the Playbook
- Run loadbalancer playbook for setup HAProxy
ansible-playbook loadbalancer.yml
2. Run myweb playbook for setup Apache webserver
ansible-playbook myweb.yml
Finally our HAProxy LoadBalancer configured ….
ThankYou
My LinkedIn: Himanshu Agrawal