Configure Haproxy and Webserver Using Ansible Role
🔅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 web server versions and solving challenges for host IP’s addition dynamically over each Managed Node in HAProxy.cfg file.
Step 1: First we have to create an Ansible role for the webserver (name: - webserver) and For load balancer (name load balancer)
Step 2: Now we have to do the following step for the configuration of the Webserver
---
# tasks file for webserver
- package:
name: "{{ item }}"
state: present
loop:
"{{ x }}"
- copy:
dest: /var/www/html/
src: index.php
- service:
name: httpd
state: started
- replace:
dest: "/etc/httpd/conf/httpd.conf"
regexp: AllowOveride None
replace: AllowOveride AuthConfig
notify: restart
- block:
- file:
path: /etc/www.passwd
state: file
rescue:
- file:
path: /etc/www.passwd
state: touch
- block:
- file:
path: "{{ httpdpasswdfile }}"
state: file
rescue:
- file:
path: "{{ httpdpasswdfile }}"
state: touch
- blockinfile:
path: "{{ httpdpasswdfile }}"
block:
AuthName "HKM"
AuthType Basic
AuthUserFile "/etc/www.passwd"
Require valid-user
ignore_errors: true
- block:
- htpasswd:
path: /etc/www.passwd
name: "{{ username }}"
password: "{{ password }}"
notify: restart
rescue:
- debug:
msg: "User and Password exist"
This is the Handlers file
---
# handlers file for webserver
#
- name: installation
service:
name: httpd
state: started
- name: restart
service:
name: httpd
state: restarted
This is Vars file for the webserver
---
# vars file for webserver
#
x:
- httpd
- php
username: vinayak
password: kmar
httpdpasswdfile: "/var/www/html/.htaccess"
Step 3: Now we have to configure the LoadBalancer
---
# tasks file for loadbalancer
- package:
name: "{{ package_name }}"
state: present
- template:
dest: "/etc/haproxy/haproxy.cfg"
src: haproxy.cfg
notify: restart
- service:
name: "{{ package_name }}"
state: started
Handlers File
---
# handlers file for loadbalancer
#
- name: restart
service:
name: haproxy
state: started
Vars File
---
# vars file for loadbalancer
#
package_name: haproxy
Step 4: To Use Both the Role we have to add the Ansible Playbook
- hosts: web
roles:
- role: "webserver"
- hosts: lb
roles:
- role: "loadbalancer"
Now We have to use the LoadBalancer ip and the port (5000 ) to get our site (192.168.6.2:5000)
Here It Asks a Password and username as we set this using Ansible Role. U can See the Above Playbook of Webserver
This is the Final Output we Receive u can see that u get our site
NOTE:- GITHUB URL :- https://github.com/vinayak04u/Webserver-and-Haproxy-using-Ansible-role