16. Ansible roles

Preview

Ansible roles allow you to classify your ansible components like variables, tasks, handler, template and files so that it can be easily shared with others and reused. In other words after you classify your content in roles, you can easily reuse them and share them with other users. Ansible roles is the topic of this video.

Ansible roles

An Ansible role has a pre-defined directory structure Inside roles directory in your project folder, with some main standard directories.

root@debian:~/ansible-project1# tree roles/hostname/
roles/hostname/
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
│   └── hostname.j2
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

I have already defined a role with the name of hostname which change the hostname of the managed nodes. We can check the directory structure of role with tree command. In a few minutes you will see how we create a role in ansible projects.

You must include at least one of these directories in each role. You can omit any directories the role does not use.

ansible roles in ansible website

As you can see there are some pre-defined directories.

tasks/main.yml – in tasks folder and in main.yml file, you copy list of your tasks. In our role we call a task that changes the hostname of device with the help of template and when a change occurs, it notifies the handler with the name “config_changed”, which shows the result of the changes.

root@debian:~/ansible-project1# cat roles/hostname/tasks/main.yml
---
# tasks file for hostname
    - name: change config
      ios_config:
        src: "hostname.j2"
        match: none
      notify: config_changed
      register: response

handlers/main.yml – in the handlers folder and in the main.yml file, copy your handlers here, as the name suggests. In our role we use a handler that shows the output of changes. We copy our handler into this folder.

root@debian:~/ansible-project1# cat roles/hostname/handlers/main.yml
---
# handlers file for hostname
    - name: "changes.."
      listen: config_changed
      debug:
        msg: "{{ response }}"

defaults/main.yml – in this folder, default variables for the role are stored. These variables have the lowest priority of any variables available, and can be easily overridden by any other variable, including inventory variables. in our role we have no variable defined in defaults folder.

vars/main.yml – this is the folder for node-independent variables. These variables have higher priority than defaults.  I have already copied as variable and domain variable in this folder. Note that host-specific and group-specific variables are stored outside of the roles directory structure, and it is suggested, still use the host_vars and group_vars directory.

root@debian:~/ansible-project1# cat roles/hostname/vars/main.yml
---
# vars file for eigrp
as: 1
domain: rayka-co.com

files– in the files folder, files are copied that the role deploys. for example configuration file of any services that are transferred in a managed-server during playbook execution. This folder is used less in network automation and it mostly practical in server automation.

templates– here you copy your template files. for our example, hostname template file is copied here.

root@debian:~/ansible-project1# cat roles/hostname/templates/hostname.j2
hostname {{ inventory_hostname }}
root@debian:~/ansible-project1#

meta/main.yml – in meta folder, you store metadata information for the role, and also role dependencies. By meta data information I mean , information regarding author information, company information and so…

access ansible codes of this course in github

this is a video-based training therefore the output of running commands are not show in the text.

You can run other roles before this rule is executed if you add them in role dependency part of this file.

We have copied our hostname role directory structure in roles folder in our project. By default, Ansible looks for roles in two locations: in a directory called roles/, in your current project folder which we have implemented and also in /etc/ansible/roles folder.

How we can create roles? You can create them manually. But with ansible-galaxy command it is easier. ansible-galaxy command which is included in Ansible installation process, allows you to create your own roles and also download roles from Ansible Galaxy shared by others which is the discussion of next video.

# cd roles
# ansible-galaxy init test

root@debian:~/ansible-project1# tree roles/test
roles/test
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

how we can use the roles? you can use the roles at the play level as we have done here or at the tasks level with “include_role” parameter when you want to call a role in a task between some other tasks.

root@debian:~/ansible-project1# cat hostname_with_roles.yaml
---
- hosts: csr1
  gather_facts: false
  connection: network_cli
#  roles:
#    - hostname

  tasks:
    - name: call a role to be run
      include_role:
        name: hostname
Back to: Ansible for Network Engineers > Ansible other Features

Leave a comment

Your email address will not be published. Required fields are marked *