Ansible Roles for Configuring Webserver and Haproxy
Tags: aws, linux, ansible, apache
Published: 2021-01-21 20:08:52

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](https://dev-to-uploads.s3.amazonaws.com/i/9gmi20st08za8n3q6w1a.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/7rzpr7gsr4q4n0hndehi.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/jido72yck70hdx8fmb31.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/afj6z7ml1vic6c04kqnw.png)

- Now go inside the file directory 

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xs4nshqxi5148io67zp3.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/hql9fi4ion4cya2i5w0y.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/nehqo3qowbimh2ahk11n.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/v090cd32fi3ckqutdczq.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/pn52rueimeeqveq8dqf0.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/i/o44d5dt9uw2i0ftb8enu.png)

#Now our both Haproxy and Webserver roles are created 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o44d5dt9uw2i0ftb8enu.png)

> Now create the ansible configuration file

Create an ansible configuration file with the extension `cfg`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4uwaj226wm9bvohzmyto.png)

> 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](https://dev-to-uploads.s3.amazonaws.com/i/ofswpiowmndb4ia1294k.png)

#Now we are ready to run the playbook

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/44ftptqc1x11w6jyxrzf.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/q6bb534p8nnduwbruj99.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9tyi993ci9jt4rrsy7je.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/otchkvoxpy0z2zza2g1o.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a65n6qqxcjf74zqg0ti8.png)

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j3iv5491pp0zj5ov87ik.png)

It is working!!

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

Thank you!!