Creating role for the “httpd” & “haproxy” and add host IP’s dynamically over each Managed Node in haproxy.cfg file.
Hello Geeks😎😎, This blog will help you to configure the webserver with a managed load balancer
What we gonna do?
→ 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.
Role for Configuring HTTPD Websever
You can use below command to create role -
ansible-galaxy role init httpd
Configure Task File
Use below code to configure main.yml file in tasks directory
---
#Task for the HTTPD- name : “Installing HTTPD Package”
package:
name: “{{package_name}}”
state: present- name : “Copy Content in Document Root”
template:
dest: “/var/www/html/index.html”
content: “Hello! Welcome…”
notify: “Restart the Service”- name : “Starting the HTTPD Service”
service:
name: “{{package_name}}”
state: started
Configure Vars File
Use below code to configure main.yml file in vars directory
Configure Handler File
Use below code to configure main.yml file in handlers directory
Role for Configuring HAProxy Load Balancer
Configure Task File
Use below code to configure main.yml file in tasks directory
---
# tasks file for the HAPROXY- name: “Installing the HAPROXY Package”
package:
name: “{{package_name}}”
state: present- name: “Configuring HAPROXY”
template:
dest: “/etc/haproxy/haproxy.cfg”
src: “/RoleHttpdHaproxy/haproxy/templates/haproxy.cfg.j2”
notify: “Restart the Service”- name: “Starting the HAPROXY Service”
service:
name: “{{package_name}}”
state: started
Configure Vars File
Use below code to configure main.yml file in vars directory
Configure Handler File
Use below code to configure main.yml file in handlers directory
Haproxy Configuration file — haproxy.cfg
Do below changes in haproxy.cfg file
Play-Book For httpd and haproxy Roles
Use below code to configure setup.yml file for httpd and haproxy roles
- hosts: myweb
roles:
- role: "httpd"- hosts: mylb
roles:
- role: "haproxy"
Inventory file for the ansible
Configuration file of ansible i.e. ansible.cfg
Run ansible playbook using below command
ansible-playbook setup.yml
Finally, Our Web — Server and Load — Balancer have been configured. We can see that IPs are dynamically added in the HAProxy Configuration File.