configure haproxy Using ansible roles
The Apache HTTP Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.
HAPROXY
HAproxy is free, open-source software that provides a high availability load balancer and proxy server for TCP and HTTP-based applications that spreads requests across multiple servers. It is written in C and has a reputation for being fast and efficient.
if you don’t know how to configure an Apache web server and HAPROXY you can read my following blog:
https://all-about-devops.blogspot.com/2021/03/configure-haproxy-using-ansible.html
RED HAT ANSIBLE
Ansible is a radically simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.
Ansible is Designed for multi-tier deployments, Ansible models your IT infrastructure by describing how all of your systems inter-relate, rather than just managing one system at a time.
RED HAT ANSIBLE ROLES
Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.
In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of the playbook allows you to logically break the playbook into reusable components.
Ansible roles have a predefined folder structure that makes it even easier to use:
The defaults directory is for defining the variable defaults. The variables in default have the lowest priority thus becoming easy to override. If the definition of a variable is nowhere else, the variable in defaults/main.yml will be used.
Files:
We use the files directory to add files that are needed by the provisioning machine, without modification. Mostly, we use the copy task for referencing files in the files directory. The most interesting part about this is that Ansible does not require a path for resources stored in the files directory when working in the role.
Handlers:
The handlers directory is used for storing Ansible handlers. Handlers are tasks that may be flagged during a play to run at the play’s completion. We can have as many and as few handlers as we need.
Meta:
We use the meta-directory to store authorship information which is useful if we choose to publish our role on galaxy.ansible.com. The metadata of an Ansible role consists of the author, supported platforms, and dependencies.
Tasks:
The task directory is where we write most of our roles which include all the tasks our role will perform. We write each series of tasks in a separate file and include them into the main.yml file in the tasks directory.
Templates:
We use the template directory to also add files to our machine(similar to the files directory). The only difference between template and file directories is that the template directory supports alteration (modification). Jinja2 language to used to create these alterations. Most software configuration files become templates.
Tests:
We can use the tests directory if we have built an automated testing process around our role. This directory contains a sample inventory and a test.yml file.
Vars:
This is where we create variable files that define necessary variables for our role. The variables defined in this directory are meant for role internal use only. Also, it is a good idea to namespace our role variable names, to prevent potential naming conflicts with variables outside of our role.
to create a role use the following command
ansible-galaxy init 'any role name'
to execute a role we need to include roles in ansible-playbook
- hosts: 'nodes name where to configure'
roles:
- 'role name'
to run this playbook use the following command
ansible-playbook 'playbook-name.yml'
Now, this much knowledge is enough to do our task. we will move towards our task
Task Objective:
🔅Create an Ansible role apache to configure Httpd WebServer.
🔅Create another ansible role haproxy to configure HAProxy LB.
🔅We need to combine both of these roles controlling web server versions
and solving challenges for host IPs addition dynamically over each Managed
nodes in HAProxy.cfg file.
To get a true feeling of automation please use playbook only do not use any command in between
NOTE: all the source code is in GitHub. the link provided in the last
Let’s Get Started
Inventory File[Data.txt]
Ansible uses this file to check host connectivity it is a kind of IP database.
[HAPROXY]
192.168.0.112 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root[WebServer]
192.168.0.113 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root
192.168.0.108 ansible_connection=ssh ansible_user=root ansible_ssh_pass=root
step 1: Connect to the managed node
to check the connectivity of the managed node run the following command.
ansible all -m ping
step 2: Create apache and HAPROXY role and put all codes to make it ready to use
I have already created both roles. the following image can explain both roles and note that all the code that we need already created in that files
apache role:
in this role, all the main code goes in tasks/main.yml, all-important variables that we need go in var/main.yml, and finally, your website or webpages will go inside templates/webpage.php.j2
here the templates are dynamic in nature which means you can use inbuilt variables of ansible or ansible_facts or user-defined variables on the webpages files
for checking web server is running or not we are using an index.php file here
in this role, all the main code goes in tasks/main.yml, all-important variables that we need go in var/main.yml, and finally, your haproxy configuration file will go inside templates/haproxy.cfg.j2
here the templates are dynamic in nature which means you can use inbuilt variables of ansible or ansible_facts or user-defined variables in the haproxy configuration file
so we will use inbuilt variable ‘groups[‘server group’]’ for adding web servers in haproxy load balancer as backend server’s and LoadBlancer_port a user-defined variable for haproxy port
step 3: Create an ansible-playbook to run this role
following is the playbook we need to create to execute our roles
- hosts: WebServerroles: - apache
- hosts: HAPROXYroles: - HAPROXY
Now execute this playbook you will output like this
Note: I forget to add firewall ports and service start task in roles so the following image will show that I have executed roles after adding these modules
Finally, we are ready with our reverse proxy that is haproxy in the following images you can see haproxy working fine, and they did work of load balancing.
to access GUI use the following URL in the browser.
http://"haproxy server ip":"listening port" Actual Backend server that is used by haproxy192.168.0.108
192.168.0.113
GitHub Link:
https://github.com/venkateshpensalwar/ARTH/tree/main/Ansible/roles
Conclusion:
we have learned how to configure haproxy Software using the ansible roles. In this way, we can see that how easily we able to configure such a complex task using ansible roles. and this is the power of ansible roles in the configuration world.
Hope this blog is helpful to you!!!!
No comments