Roles in Ansible

Share via:

What are Ansible Roles?

Ansible roles consists of many playbooks.
Roles are way to group multple tasks together into one container to do the automation in very effective manner with clean directory structure.
A role directory structure contains directories: defaults, vars, tasks, files, templates, meta, handlers. Each directory must contain a main.yml file which contains relevant content.
Ansible Roles are independent of each other. Execution of one role doesn’t depend on others.
You can even modify and personalize these roles according to your requirements.
Adding more & more functionality to the playbooks will make it difficult to maintain in a single file.
We can organize playbooks into a directory structure called roles.

Roles Directory Structure
The directory structure for roles is essential to create a new role.
Using Ansible Roles, expect files to be in a certain file structure.
The most confusing part of using roles is understanding the file hierarchy.
Ansible provides a feature called Ansible Galaxy that helps you play with roles.
We havve seen a directory called roles under /etc/ansible.
That directory exists exactly for this reason.
We can create different roles inside this directory.

Directories in Ansible Roles
defaults
Contains default variables for the role.
Variables in default have the lowest priority so they are easy to override.
vars
Contains variables for the role.
Variables in vars have higher priority than variables in defaults directory.
tasks
Contains the main list of steps to be executed by the role.
files
Contains files which we want to be copied to the remote host.
We don’t need to specify a path of resources stored in this directory.
templates
Contains file template which supports modifications from the role.
We use the Jinja2 templating language for creating templates.
meta
Contains metadata of role like an author, support platforms, dependencies.
handlers
Contains handlers which can be invoked by “notify” directives and are associated with service.
Every task directory must consist of a main.yml file where the actual code for that particular role is written.

Steps to Follow

  1. Create a new role “ktexperts-Role” in “/etc/ansible/roles” directory.
  2. Go to Role “/etc/ansible/roles/Ktexperts-Role”.
  3. Open the file “main.yml” in the directory “tasks” and write code to install the package “httpd”.
  4. Go inside home directory “/home/kt-ansible”.
  5. Create a cookbook “master.yml”.
  6. Run the playbook “master.yml” to call all the playbooks inside the role “ktexperts-Role”.
  7. Verify the package “httpd” inside 2 hosts.

1. Create a new role “ktexperts-Role” in “/etc/ansible/roles” directory

We can create a role using ansible-galaxy init command inside /etc/ansible/roles.

ansible-galaxy —>It is the command to create roles using templates.

init —->Initialize the role.

Ktexperts-Role —-> It is the name of the role.

See the list of directories under “/etc/ansible/roles/Ktexperts-Role”

We have got a clean directory structure with ansible-galaxy command.

Each directory should have main.yml file, which contains the relevant content.

2. Go to Role “/etc/ansible/roles/Ktexperts-Role”

We can see the file “main.yml” in tasks directory. we got this file by default when you create a role by using the ansible-galaxy init command.

3. Open the file “main.yml” in the directory “tasks” and write code to install the package “httpd”

Note
:wq! —– to quit.

See the content of the file “main.yml” in the directory “task”

Note
:wq! —– to quit.

4. Go inside home directory “/home/kt-ansible”

5. Create a cookbook “master.yml”

Note
:wq! —– to quit.

6. Run the playbook “master.yml” to call all the playbooks inside the role “ktexperts-Role”

7. Verify the package “httpd” inside 2 hosts

Thank you for giving your valuable time to read the above information.
Follow us on 
Website  www.ktexperts.com
Facebook Page KTexperts

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment