Combine Two Ansible Role And Controlling Webserver

🔅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.

Inventory file: role.txt

Ansible configuration file: /etc/ansible/ansible.cfg

Step 1 — Create myappacherole:

ansible-galaxy role init myapache

Tasks: myappacherole/tasks/main.yml-

---
# tasks file for myappacherole
- name: "configure yum "
yum_repository:
name: dvd1
description: appstream
baseurl: file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/AppStream
file: rehel
gpgcheck: no
- name: "configure yum"
yum_repository:
name: dvd2
description: baseos
baseurl: file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/BaseOS
file: rehel
gpgcheck: no
- name: "install httpd"
package:
name: "httpd"
state: present
- name: "stop firewalld"
service:
name: firewalld
state: stopped
enabled: False
- name: "start httpd service"
service:
name: httpd
state: started
- name: "copy html file"
template:
src: "index.html.j2"
dest: "/var/www/html/index.html"
notify: codechange

Template File: myappacherole/templates/index.html.j2

<!DOCTYPE html>
<html>
<body>
<h1 style="background-color:DodgerBlue;">WEBSERVER(HAPROXY)</h1>
<h2>MY WEBSERVER IP: </h2><h1 style="color:red;"><b>{{ ansible_facts['default_ipv4']['address'] }}</b></h1>
</body>
</html>

Handlers: myappacherole/handlers/main.yml

---
# handlers file for myappacherole
- name: codechange
service:
name: "httpd"
state: "restarted"

Step 2— Create loadbalancerrole

ansible-galaxy role init loadbalancerrole

Tasks: myappacherole/tasks/main.yml-

---
# tasks file for loadbalancerrole
- name: "configure yum "
yum_repository:
name: dvd1
description: appstream
baseurl: file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/AppStream
file: rehel
gpgcheck: no
- name: "configure yum"
yum_repository:
name: dvd2
description: baseos
baseurl: file:///run/media/root/RHEL-8-0-0-BaseOS-x86_64/BaseOS
file: rehel
gpgcheck: no
- name: "install haproxy"
package:
name: "haproxy"
state: present
- name: "configur /etc/haproxy/haproxy.cfg file"
template:
dest: "/etc/haproxy/haproxy.cfg"
src: "haproxy.cfg.j2"
notify:
- Restart haproxy
- name: "start haproxy services"
service:
name: "haproxy"
state: started

Template File: loadbalancerrole/templates/haproxy.cfg.j2

Now we will edit haproxy.cfg file so that it will add the IP automatically as soon as any new IP for server joins the Inventory

backend app
balance roundrobin
{% for i in groups['server'] %}
server app{{ loop.index }} {{ i }}:80 check
{% endfor %}

Handlers: loadbalancerrole/handlers/main.yml

---
# handlers file for loadbalancerrole
- name: Restart haproxy
service:
name: "haproxy"
state: "restarted"

Step 3 — Now we will create a playbook to run these twon roles:

role.yml-

- hosts: haproxy
roles:
- loadbalancerrole
- hosts: server
roles:
- myappacherole

Step 4 — Now we will run our playbook:

ansible-playbook role.yml

Now we will check our webserver :

http://192.168.43.131:8080

Thank You!!!