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
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.
- 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
- Now go inside the handlers directory
Inside the handlers directory, you will find the main file edit that file, and add handlers to it.
- Now go inside the file directory
Inside the file, directory add the file that you want to upload in the target node
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.
- 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.
- 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
- 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
- Now go inside the handlers directory
Inside the handlers directory, you will find the main file edit that file, and add handlers to it.
Now our both Haproxy and Webserver roles are created
Now create the ansible configuration file
Create an ansible configuration file with the extension cfg
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
Now we are ready to run the playbook
To run the playbook use the command ansible-playbook <playbook-name>
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
Webserver is running, now let's check is it working
It is working great!!
Now let's check for Haproxy
To check Haproxy is running or not use the command systemctl status haproxy
In both the Target once it is running, now let's check is it working
It is working!!
So that's how we can use Ansible roles to configure Webserver and Haproxy.
Thank you!!
Top comments (0)