top of page
Search
Writer's pictureroshnipatil1314

Ansible Roles

Welcome to my new technical blog, Through this blog, I'm going to describe what is ansible roles, how it helps to manage the playbook of ansible and how to create Ansible roles


What are the ansible roles...

Roles are a way to group multiple tasks into one container to do the automation in a very effective manner with clean directory structures. It contains a set of tasks and additional files for a certain role which allow you to break up the configurations.


How it helps ...

Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure. Once you group your content in roles, you can easily reuse them and share them with other users.

 

Here I am going to configure two roles one for webserver and another one for load balancer (reverse proxy)


Role for Webserver:-


First I am going to create one role, So the command is:-

 
 sudo ansible-galaxy role init <role_name>	
 	


If you store your roles in a different location then you have to first update your ansible.cfg file and add the path of the role.



Now change your directory to the role directory which is nothing but a webserver in my case.


Files directory contains the files or configuration files which we need to use. So, in this directory, I

am going to create one file i.e; "index.php". This file is nothing but my webpage.



Tasks directory contains the main.yml file which contains all the tasks. So in this directory, I am going to add a task.



Vars directory contains all the variables which we need to use in our task.




Here role for configuring the webserver has been done successsfully.

Now I am going to create a role for the load balancer.


Role for Load Balancer:-


Here I am going to create one role, So the command is:-


 ansible-galaxy role init <role_name>	
 	

The role has been created successfully, Now change your directory to the role directory which is nothing but a load balancer in my case.

The template directory contains all the templates which we need to use. So, in this directory, I

have one template i.e; "haproxy.cfg.j2". With the help of this template, you can configure your load balancer(reverse proxy).


Now in the task directory, I am going to add a task.

Now move to the handler directory.



With the help of the handler, we will restart the haproxy service only when the configuration file is changed.

So here a role for configuring load balancer has been done successfully.


 

Now I am going to combine both roles. So here I am creating one playbook which is named as setup.yml which contains all the roles



Here I am going to run the playbook " setup.yml". this will run the roles on the managed nodes.


Command to run the playbook:-
ansible-playbook 

Now let's check it is working or not.

So now I am going to access our web server with the IP of the load_balancer and the port number 5000 which has bound in the haproxy.cfg.j2 file.


Here you can see we are connected to 192.168.43.251 which is nothing but the IP of the webserver

So here, our whole setup is done successfully.


Thanks for reading🙌🙌

33 views0 comments

Recent Posts

See All

Comments


Post: Blog2_Post
bottom of page