Chapter 8: Simplifying Playbooks with Roles and Collections

Describing role structure

Task files let you reuse a list of tasks. A role goes further: it bundles tasks with the variables, templates, files and handlers they need, in a standard layout that Ansible understands. A well-made role can be dropped into any project and used with one line.

What a role is

A role is a directory with a fixed structure. Each subdirectory has one job, and Ansible loads main.yml from each automatically. Because the layout is always the same, anyone who has seen one role can find their way around another.

Roles pay off in three ways:

The role layout

Select a file to see what it is for:

No individual file is mandatory: a role may provide tasks, variables, handlers, or files. A role that runs tasks normally uses tasks/main.yml. Leave out, or delete, any directory a role does not use.

DirectoryHolds
tasksThe tasks the role runs
defaultsDefault variable values, meant to be overridden
varsVariables internal to the role, not meant to be overridden
handlersHandlers the role's tasks notify
templatesJinja2 templates used by the role's tasks
filesStatic files used by the role's tasks
metaInformation about the role, including its dependencies

Inside a role, template and copy tasks refer to their source by file name alone. Ansible looks in the role's own templates/ and files/ directories first.

Using a role in a play

List roles under the roles keyword of a play:

- name: Web servers run the site
  hosts: web
  roles:
    - web_site
    - monitoring_agent

Ansible looks for each role in a roles/ directory next to the playbook, then in the directories of the roles_path setting (by default ~/.ansible/roles, /usr/share/ansible/roles and /etc/ansible/roles).

To set a role's variables for one play, give the role entry a vars section:

  roles:
    - role: web_site
      vars:
        web_site_title: Staff intranet

The order things run in

A play can contain roles and its own tasks. Ansible does not run them in the order they are written. It runs pre_tasks, then roles, then tasks, then post_tasks, with handler flushes after pre_tasks, after roles and tasks together, and after post_tasks. Select a step to see where it comes from:

Use pre_tasks for something that must happen before any role, such as taking a host out of a load balancer, and post_tasks for the matching step afterwards.

Variables in a role

A role has two places for variables, and they behave very differently:

The role's settings, with sensible starting values. They have the lowest precedence of anything in Ansible, so inventory, play variables and the command line can all override them.

Values the role needs internally, such as a path that must not change. They have high precedence: inventory and play variables do not override them.

Tick the places where a variable is set and see which value the role ends up with:

Takeaway

Explain what a role is and why roles make playbooks easier to maintain. Use the chapter lab to check this on a real host.