HAPRoxy and Http Ansible Roles

Yogesh
3 min readMar 20, 2021

--

In this blog, I’ll describe how I went about creating an Apache HTTP web server and an HAProxy role and how to use them together in a playbook. I’ve implemented and tested the roles and playbook on RHEL 8 OS.

Ansible Roles📜

Ansible roles let one automatically load related vars_files, tasks, handlers, etc… based on a known file structure. Once you group your content into roles, you can easily reuse them and share them with other users.

Steps 🤔

  • Create an Ansible role webserver that configures the Apache HTTP web server.
  • Create another ansible role mylb that configures HAProxy Load Balancer.
  • Then combine both of these roles which solve the challenge for adding host IPs dynamically for each Managed Node in the HAProxy configuration file.

Implementation 🤓🙂

Role to configure Apache HTTP webserver 🧐

✔️ Initialize a role myapache to configure the Apache webserver.

  • ansible-galaxy role init myapache

✔️ Write the following in main.yml inside tasks folder of this role to configure the Apache HTTP web server. The names of the tasks are pretty clear to understand what it is doing.

---
# tasks file for webserver
- name: "Install apache webserver"
package:
name: "httpd"
state: "present"
- name: "Copy files"
copy:
src: "Aincrad.jpg"
dest: "/var/www/html/"
- name: "Copy template files"
template:
src: "index.html.j2"
dest: "/var/www/html/index.html"
- name: "Write a firewall rule for webserver"
firewalld:
port: "80/tcp"
state: "enabled"
- name: "Start apache webserver"
service:
name: "httpd"
state: "started"

Note: Aincrad.jpg and index.html.j2 are the file(image) and template file(web page) in the tasks and templates folder of the role respectively.

✔️ So we are done with the Apache HTTP web server role. Yeah, you read it right, its done! as simple as that!! This is how one can get started in making a simple role. You can also create more complex roles by using varsand handlers , which I will demonstrate in the next role.

Role to configure HAProxy 🧐

✔️ Initialize another ansible role myloadbalancer to configure HAProxy LB.

  • ansible-galaxy role init myloadbalancer

✔️ Write the following in main.yml inside tasks folder of this role to configure the HAProxy role. Again the names are pretty clear.

---
# tasks file for mylb
- name: "Install haproxy"
package:
name: "haproxy"
state: "present"
- name: "Copy haproxy config file"
template:
src: "haproxy.cfg.j2"
dest: "/etc/haproxy/haproxy.cfg"
notify: "Restart haproxy server"
- name: "Write a firewall rule for webserver"
firewalld:
port: "{{ haproxy_port }}/tcp"
state: "enabled"
- name: "Start haproxy"
service:
name: "haproxy"
state: "started"

Note: haproxy.cfg.j2 is an HAProxy template file(in the templates folder of the role) that dynamically adds the managed nodes to the backend pool of the HAProxy load balancer. To know more about it check my GitHub.

✔️ ️️In the above task notice that haproxy_port is a variable. We can define it in the vars folder of the role as shown below. You can also use other types of variables like lists and dictionary as you normally do in a playbook under the vars section.

---
# vars file for mylb
haproxy_port: 5000

✔️ In the above task, we can see that “Copy haproxy config file” task has a handler (basically notifies another task to run when this task is in changed status) named “Restart haproxy server” . So, where did we define this 🤔? Yes, you guessed it right like the vars folder there is another folder called handlers in the role. This is shown below.

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

That is all about creating roles for now. But how do we use them? Let’s find out!

Using the above roles 💻

✔️️ Ansible Roles are very simple to use in a playbook. We just need to use the roles keyword and pass the name of the role as an argument as shown below.

- hosts: webservers
roles:
- webserver
- hosts: load_balancer
roles:
- mylb

There is also another way to use roles by using include_role . That offers some more features over this. I previously used it in a role that provisions and configures K8S multi-node cluster over AWS. You can check it out here.

You can find all the roles and playbook on my GitHub.

Thank You! 😇🥰

--

--