DEV Community

Cover image for Ansible Roles for Configuring Webserver and Haproxy
Nitesh Thapliyal
Nitesh Thapliyal

Posted on

Ansible Roles for Configuring Webserver and Haproxy

Hello everyone,

In this blog, we will create an ansible role to configure the Webserver and Haproxy.

First, we should know what is Ansible Role

Ansible Role:

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.

Now let's get started:

First create a role for configuring Webserver

To create a role use command ansible-galaxy role init Webserver

  • Now go inside the directory Webserver

Alt Text

You will find subdirectories inside the main directory

  • Now go to the task directory

Inside the task, directory edit the main.yml file and write the play.

Alt Text

  • Now go inside the var directory

Inside the var directory, you will find the main.yml file, edit that file to add variables to it

Alt Text

  • Now go inside the handlers directory

Inside the handlers directory, you will find the main file edit that file, and add handlers to it.

Alt Text

  • Now go inside the file directory

Inside the file, directory add the file that you want to upload in the target node

Alt Text

That's it for the Webserver role!!

Now create the Haproxy role

To create the Haproxy role use the command ansible-galaxy role init Haproxy

After you run the command a directory will be created.

Go inside the directory Haproxy and you will find the subdirectories inside it.

Alt Text

  • Go inside the task directory

Inside the task directory, you will find the main.yml file, edit that file to configure the haproxy in the target node.

Alt Text

  • Now go inside the variable directory

Inside the variable directory, you will find the main.yml file, edit that file to add variables to it

Alt Text

  • Now go inside the template directory

Inside the template, directory add the template that you want to be in the target node, in our case it will be the haproxy.cfg.j2 file

Alt Text

  • Now go inside the handlers directory

Inside the handlers directory, you will find the main file edit that file, and add handlers to it.

Alt Text

Now our both Haproxy and Webserver roles are created

Alt Text

Now create the ansible configuration file

Create an ansible configuration file with the extension cfg

Alt Text

Now Create an inventory file

In inventory, file add the IP addresses of your target node

Now Create a playbook

Create a playbook that will run the roles

Alt Text

Now we are ready to run the playbook

To run the playbook use the command ansible-playbook <playbook-name>

Alt Text

Our Playbook ran without any error, now let's check if our Haproxy and webserver is configured or not

check for the Webserver

To check whether Webserver is configured if not use command systemctl status httpd

Alt Text

Webserver is running, now let's check is it working

Alt Text

It is working great!!

Now let's check for Haproxy

To check Haproxy is running or not use the command systemctl status haproxy

Alt Text

Alt Text

In both the Target once it is running, now let's check is it working

Alt Text

It is working!!

So that's how we can use Ansible roles to configure Webserver and Haproxy.

Thank you!!

Oldest comments (0)