// learn Β· linux Β· rhce Β· 11 min

Ansible roles.

A role bundles tasks, handlers, templates, files, and variables into one reusable directory layout β€” and Ansible auto-loads each subdir's main.yml. Watch the tree light up subdir by subdir, then pull a community role off Galaxy with a requirements.yml lockfile.

Ansible roles animated tutorial. The standard role directory layout β€” tasks, handlers, templates, files, vars, defaults, and meta main.yml files that Ansible auto-loads. Using roles in a play with parameters, import_role versus include_role, defaults versus vars variable precedence, installing community roles and collections from Ansible Galaxy, pinning them in requirements.yml, role dependencies in meta/main.yml, and Fully-Qualified Collection Names (FQCN). RHCE EX294 ready.
// ansible Β· roles & collections

A role is just a fixed directory layout Ansible knows how to auto-load. Watch a role's tree light up subdir by subdir β€” tasks, handlers, templates, defaults vs vars β€” then pull one off Galaxy with requirements.yml.

// before you start
you should know
  • Β·Comfortable writing Ansible playbooks and tasks
  • Β·Know what handlers and templates (.j2) are
  • Β·Basic ansible.cfg / inventory familiarity
by the end you'll

Scaffold a role with the standard layout, use it in a play with parameters, place variables correctly in defaults vs vars, and install community roles and collections from Galaxy via requirements.yml.

pace: 11 minutes

ansible β€” step 1 / 6 Β· the idea
πŸ“¦ roles/webserver/
β”œβ”€ tasks/main.ymlauto-loaded
the work β€” runs when the role is applied
β”œβ”€ handlers/main.ymlauto-loaded
handlers you notify (restart httpd …)
β”œβ”€ templates/
.j2 files β€” referenced by basename
β”œβ”€ files/
static files β€” src for the copy module
β”œβ”€ vars/main.ymlauto-loaded
HIGH-precedence vars (hard to override)
β”œβ”€ defaults/main.ymlauto-loaded
LOWEST-precedence vars (user-overridable)
β”œβ”€ meta/main.ymlauto-loaded
metadata + role dependencies
β”‚ auto-loaded into the play β”‚
site.yml β€” a play
- hosts: webservers
  roles:
    - webserver
↑ each subdir's main.yml is auto-loaded β€” no include paths

Why roles β€” stop copy-pasting

A role is a self-contained, reusable bundle of tasks, handlers, templates, files, and variables that follows a STANDARD directory layout. Drop a role into a project and Ansible auto-loads main.yml from each subdir β€” there are no include paths to wire up by hand. Roles keep your playbooks short: a 200-line web setup collapses to one line, `roles: [webserver]`. And because the layout is a fixed convention, a role you write and a role pulled off Ansible Galaxy plug in exactly the same way.

$ansible-galaxy init webserver
$ls roles/webserver/
$roles:\n - webserver
// key insight

A role is just a convention β€” a fixed directory layout where Ansible auto-loads tasks, handlers, templates, and defaults from each subdir's main.yml. Learn the layout once and every role β€” yours or one pulled off Galaxy β€” drops into a play with a single line: roles: [webserver].

// exam-ready Β· role layout, galaxy & collections
$ansible-galaxy init NAME
$roles/NAME/tasks/main.yml
$handlers/main.yml
$templates/ (by basename)
$files/ (copy src)
$vars/main.yml
$defaults/main.yml
$meta/main.yml
$roles:\n - role: NAME\n k: v
$- import_role: name=NAME
$- include_role: name=NAME
$ansible-galaxy role install X
$ansible-galaxy install -r requirements.yml
$ansible-galaxy collection install ns.coll
$ns.coll.module (FQCN)
// check yourself
4 quick questions
Q1

Which role subdir is auto-loaded as the role's main task list?

Q2

You want a role variable users can easily override from inventory. Put it in defaults/ or vars/?

Q3

What does `ansible-galaxy init webserver` do?

Q4

import_role vs include_role β€” what's the difference?

These aren't graded β€” they're just for active recall, which is what actually makes the lesson stick.

πŸ”’
// next: secrets

Ansible Vault

Roles and group_vars often hold passwords and keys. Learn to encrypt sensitive data right in your repo with Ansible Vault β€” so secrets ship safely beside your playbooks.

open β†’
// more in systems

keep going β€” these pair well with what you just learned.

see all systems β†’
back to RHCSA / RHCE trackall lessons