Ansible Roles


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 []
  • For example see below:
    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
          #  <-- 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
     - 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.


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