Roles in Ansible

In this post we are going to understand What are Ansible Roles and How do we implement one.

So, let’s get started,

What are Ansible Roles?

So as per Ansible Official Doc, Roles let you automatically load related vars_files, tasks, handlers, and other Ansible artifacts based on a known file structure.

Now let’s us keep this definition short and understandable, Roles are way to organise playbook, it consists of fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

Role directory structure

An Ansible role has a defined directory structure with seven main standard directories. You must include at least one of these directories in each role.

Directory Structure:

  • tasks/main.yml – the main list of tasks that the role executes.
  • handlers/main.yml – handlers, which may be used within or outside this role.
  • library/ – modules, which may be used within this role (see Embedding modules and plugins in roles for more information).
  • defaults/main.yml – default variables for the role (see Using Variables for more information). These variables have the lowest priority of any variables available, and can be easily overridden by any other variable, including inventory variables.
  • vars/main.yml – other variables for the role (see Using Variables for more information).
  • files/main.yml – files that the role deploys.
  • templates/main.yml – templates that the role deploys.
  • meta/main.yml – metadata for the role, including role dependencies.

So, know we got fair understanding of roles, let’s have hands on to understand better,

Now will be writing roles to install tree on the remote machine.

Create a role with the name tree

$ansible-galaxy init tree

This will create a folder structure like above when you navigate inside tree, and do ls.

Navigate to tasks and open main.yml

$cd tree/tasks
$vi main.yml

Now write following code.


# tasks file for tree
- name: Install tree
     name: "{{tree_var_name}}"
     state: present
     update_cache: yes

Here we are using tree_var_name from the variable directly, which we will be defining in variables.

Now navigate to vars directory under the role.

$cd ../vars
$vi main.yml

Update the variable value.

# vars file for tree
tree_var_name: tree

That’s it save the file.

Create inventory file under the role, you can create anywehere and update the servers, ill create under the role tree.

Now return back to the root of role tree and create a file maintree.yml

$ cd../..
$ vi maintree.yml

Now update the file with below code.

- hosts: all
  become: yes
  become_method: sudo
    #variable needed during node installation
      - tree

Here under the role, we are calling tree role to install tree on the servers named under hosts file.

Run the playbook by executing below command.

$ansible-playbook -i tree/hosts maintree.yml

That’s it sit back and relax untill Ansible does it’s work.

Github Link:

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s