Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it's configuration file automatically👨‍💻

Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it's configuration file automatically👨💻

Task Description 📃

12.1 Use Ansible playbook to Configure Reverse Proxy i.e. Haproxy and update it's configuration file automatically on each time new Managed node (Configured With Apache Webserver) join the inventory.


WHAT IS HAProxy?

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications.

Advantages of using HAProxy

  1. It is particularly suited for very high traffic web sites and powers quite a number of the world's most visited ones.
  2. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the net.

No alt text provided for this image


Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms.

What is Ansible?

Ansible is an open source community project sponsored by Red Hat, it's the simplest way to automate IT.

Advantages of Ansible

  1. Ansible seamlessly unites workflow orchestration with configuration management, provisioning, and application deployment in one easy-to-use and deploy platform.
  2. Centralizing configuration file management and deployment is a common use case for Ansible, and it’s how many power users are first introduced to the Ansible automation platform.

ANSIBLE PLAYBOOK

  1. Playbooks record and execute Ansible’s configuration, deployment, and orchestration functions. They can describe a policy you want your remote systems to enforce, or a set of steps in a general IT process.
  2. Playbooks are designed to be human-readable and are developed in a basic text language.
  3. Ansible uses Jinja2 templating to enable dynamic expressions and access to variables. 
  4. All templating happens on the Ansible controller before the task is sent and executed on the target machine. This approach minimizes the package requirements on the target (jinja2 is only required on the controller).

Configuration file for ansible

  1. If installing Ansible from a package manager, the latest ansible.cfg file should be present in /etc/ansible.
  2. Certain settings in Ansible are adjustable via a configuration file (ansible.cfg). 

Inventory file

  1. The inventory file can be in one of many formats, depending on the inventory plugins you have. The most common formats are INI and YAML.

How Ansible works?

  1. Ansible works by connecting to your nodes and pushing out small programs, called "Ansible modules" to them.
  2. These programs are written to be resource models of the desired state of the system. Ansible then executes these modules (over SSH by default), and removes them when finished.

No alt text provided for this image


  1. Your library of modules can reside on any machine, and there are no servers, daemons, or databases required.

LET'S Start the task

  1. This is our system for load balancer.

No alt text provided for this image


2. System for Webserver 1

No alt text provided for this image

3. System for Webserver 2

No alt text provided for this image

4. After knowing the ip addresses of the systems whom we have to configure as load balancer and web servers add these ip's in the inventory file.

No alt text provided for this image

5. Before running the ansible playbook in the System Load Balancer.

No alt text provided for this image

6. Before running the ansible playbook in "Webserver 1" and "Webserver 2".

No alt text provided for this image
No alt text provided for this image

7. Ansible playbook in the controller node.

No alt text provided for this image
No alt text provided for this image

8. Configuration file for the haproxy server.

No alt text provided for this image

With the last 3 lines of code it will automatically update the configuration file each time new Managed node (Configured With Apache Webserver) join the inventory.

9. After running the ansible playbook, httpd services are running in both the webservers.

No alt text provided for this image


No alt text provided for this image


10. After running the ansible playbook ,HAProxy is installed and services are running in the "Load Balancer".

No alt text provided for this image

11. Output of the ansible playbook.

No alt text provided for this image
No alt text provided for this image

12. We will check the configuration file of the HAProxy in the system configured as Load Balancer.

No alt text provided for this image
No alt text provided for this image

We can see that the ip address of the webservers have been added.

13. Let's connect to the ip of the load balancer and it will redirect us to the webservers.

No alt text provided for this image

Thank you .

To view or add a comment, sign in

Others also viewed

Explore content categories