Ansible Roles

Introduction

Roles Overview

  • Roles are a way to organize playbooks components into separate files. These components could be other playbooks, tasks, templates, files or variables. These a inserted into a playbook using the keyword roles.
  • Roles are similar to using include where you specify in a playbook which file to include that may represent playbooks components such as tasks, other playbooks, templates…etc. .
  • The main difference between roles and include is that roles provide a more scalable organization.
  • Roles are defined by a filesystem structure.
  • For best practice see the recommended filesystem structure for roles [http://docs.ansible.com/playbooks_best_practices.html#directory-layout]
  • For example see below:
roles/
    common/               # this hierarchy represents a "role"
        tasks/            #
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #
            main.yml      #  <-- handlers file
        templates/        #  <-- files for use with the template resource
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #
            main.yml      #  <-- variables associated with this role
        defaults/         #
            main.yml      #  <-- default lower priority variables for this role
        meta/             #
            main.yml      #  <-- role dependencies

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""
  • For example to use a role in a playbooks:
---
- hosts: edgerouter
  roles:
     - routers
  • If there was a file called /roles/routers/tasks/main.yml, the above playbook would import those tasks.
  • The same applied if there was a file called /roles/routers/vars/main.yml, the above playbook would import those variables.

Comments

rating: 0+x
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License