What are Roles in Ansible?

0
103
Spread the love
  • 28
  • 34
  • 28
  • 38
  •  
  •  
  •  
  • 12
  •  
  •  
    140
    Shares

Ansible roles
Ansible roles

Roles in Ansible

Rolls are also next level abstraction of a Playbook.As and when our project gets bigger we will get larger files base or a lot of files to work on. Roles comes handy that allows us to group files together in a defined format.

Roles also allows us to place the variables, files, tasks, templates, handlers in a folder for future use. The integration of these are really simple.

Ansible further allows us to define roles inside of a role (it resembles a tree).Roles are primarily services but they can also be Daemons, options, or characteristics.

Why roles are need in Ansible?

Playbook are split into bits and pieces and places in different directories.

Roles help us in-

  • Bringing father modularity
  • Assembling changes
  • Troubleshooting becomes easy

To manage roles, we need to perform the following tasks-

  1. Create a folder called roles with the Playbook.
  2. In roles folder create subfolders for each role we wish to create.
  3. In each role folder create files, handlers, meta, tasks, templates and vars folders. If we do not need to use them, we can create as per our need.
  4. In Playbook, add the roles by the keyword roles.
Directory Details
tasks The task folder contains main.yml file that includes list of tasks for that particular role.
files The files folder is the by default locations for files for the role that are used by the copy or script module.It contains all static content files.
templates The template folder contains the templates .Templates are written using jjinja2 format for roles.
handlers The handlers folder again contains main.yml file that further contains all handlers for the role.
vars The vars folder contains main.yml that further contains variables for the role.
meta The meta folder contains a main.yml file containing settings for a role along with its dependencies.It is data about the role.The major information are author, name, description, company, license, main_ansible_version etc
default The default folder contains the default variables to be sent to the roles.It contains a main.yml file that contains all default values that can override the initial values.
README.md Installation file.

Assume that we have 100 tasks in a single Playbook. Reading, maintaining, troubleshooting are tough for this large Playbook.

>cd/etc/ ansible will take us to roles folder.It is an empty folder initially.We can set up the roles in ansible.cfg file.In configuration file go to roles_path variable.You can see

roles_ path=/etc/ansible/roles: user/share/ansible/roles

Ansible looks for roles definition in these two directories.
To create roles folder structure we need to provide the below command

> ansible_ galaxy init myRoles --offline

Where galaxy module helps us to create the roles setup init helps in initialise.
myRoles is the name of the role.Offline-> if we do not give this command, ansible will to go internet and see in repository for a role settings.

Hands on on Roles

  • go to vars
>vi main.yml
pname:"httpd"
role1:"Linux architect"
  • go to files

Place the static files that we want to copy.

  • go to template

Create an index.html file

<html>
<head>
This is {{ansible_ hostname}}
</head>
<body>
<h1> this web page is hosted on {{ansible_ hostname}} </h1>
<h2> the copy time is {{ansible_ date_ time.time}}</h2>
<h3> contact the admin_ {{name1}} Role={{role1}}</h3>
// the role will come from vars file as vars overrides the default.
</body>
</html>
  • go to meta
>vi main.yml

Change author

author: techtravelhub
description: test

edit main.yml

>vi main.yml
---
 -name: restarting the service
   service:
     name:"{{pname}}"
     state: restarted
...
  • let’s create tasks

To create modularity let’s create several tasks here.

To create install.yml

---
  -name: installing the{{pname}} package
    yum:
      name:"{{pname}}"
      state:"installed"
...

To create service.yml

---
  -name: starting the service
    service:
     name:"{{pname}}"
     state:"started"
...

To create configure.yml

---
  -name: copying the configuration file
    copy:
      src:"httpd.conf"
      dest:"\etc\httpd\config\httpd.conf"
    notify:
     -"restarting the service"
   -name: copying the index file
     template:
       src:"index.j2"
       dest:"/var/www/html/index.html"
     notify:
     -restarting the service
...

Now edit main.yml file under task folder

-include: Install.yml
-include: Service.yml
-include: configure.yml

We can use import_ task as an alternative to include.Now the final task to create a Playbook.

-webserver.yml-

---
  -hosts: web
   user:ansadmin
   become:true
   become_ method:sudo
  roles:
   -myRole
...

Now to run this Playbook we need to use the below command

> ansible_playbook webserver.yml

Note: http://galaxy.ansible.com is having a lot of predefined rules. We can download and configure them to further use.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 28
  • 34
  • 28
  • 38
  •  
  •  
  •  
  • 12
  •  
  •  
    140
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here