TExcelTAD

Roles

A role packages everything needed to configure one thing (nginx, a firewall, the linkly app) into a folder with a fixed layout. Plays then just list roles. Roles are how Ansible code is shared between playbooks, teams and, through Galaxy, the world.

The layout

ansible-galaxy role init creates the skeleton:

bash · try it yourself
mkdir roles && cd roles
ansible-galaxy role init nginx_site
tree nginx_site
- Role nginx_site was created successfully
nginx_site
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

9 directories, 8 files
Folder Holds Loaded
tasks/main.yml the role's tasks automatically
handlers/main.yml its handlers automatically
defaults/main.yml settings users may override (lowest precedence) automatically
vars/main.yml internal constants (high precedence) automatically
templates/ .j2 files for template: searched by src:
files/ static files for copy: and script: searched by src:
meta/main.yml metadata, dependencies, Galaxy info automatically
meta/argument_specs.yml typed input documentation and validation automatically (next lesson)
tests/ an old-style test playbook; use Molecule instead

Delete what you don't use: an empty vars/main.yml or README boilerplate is noise.

A small role, used three ways

bash · try it yourself
mkdir -p roles/motd/{tasks,defaults,templates,handlers}
cat > roles/motd/defaults/main.yml <<'YML'
---
motd_path: /home/learner/motd
motd_team: platform
motd_contact: ops@example.com
YML
cat > roles/motd/templates/motd.j2 <<'J2'
{{ ansible_managed | comment }}
This server is managed by the {{ motd_team }} team.
Questions: {{ motd_contact }}
J2
cat > roles/motd/tasks/main.yml <<'YML'
---
- name: Write the message of the day
  ansible.builtin.template:
    src: motd.j2
    dest: "{{ motd_path }}"
    mode: "0644"
  notify: Motd changed
YML
cat > roles/motd/handlers/main.yml <<'YML'
---
- name: Motd changed
  ansible.builtin.debug:
    msg: "motd updated at {{ motd_path }}"
YML
cat > site.yml <<'YML'
- name: 1. roles keyword (runs before the play's tasks)
  hosts: localhost
  gather_facts: false
  roles:
    - role: motd
      vars:
        motd_team: data

- name: 2. include_role (a task, dynamic)
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Second motd
      ansible.builtin.include_role:
        name: motd
      vars:
        motd_path: /home/learner/motd2

- name: 3. import_role (a task, static)
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Third motd with defaults
      ansible.builtin.import_role:
        name: motd
      vars:
        motd_path: /home/learner/motd3
YML
ansible-playbook site.yml | grep -E '^(PLAY|TASK|RUNNING|changed|included)|"msg"'
cat /home/learner/motd
PLAY [1. roles keyword (runs before the play's tasks)] *************************
TASK [motd : Write the message of the day] *************************************
changed: [localhost]
RUNNING HANDLER [motd : Motd changed] ******************************************
    "msg": "motd updated at /home/learner/motd"
PLAY [2. include_role (a task, dynamic)] ***************************************
TASK [Second motd] *************************************************************
included: motd for localhost
TASK [motd : Write the message of the day] *************************************
changed: [localhost]
RUNNING HANDLER [motd : Motd changed] ******************************************
    "msg": "motd updated at /home/learner/motd2"
PLAY [3. import_role (a task, static)] *****************************************
TASK [motd : Write the message of the day] *************************************
changed: [localhost]
RUNNING HANDLER [motd : Motd changed] ******************************************
    "msg": "motd updated at /home/learner/motd3"
PLAY RECAP *********************************************************************
#
# Ansible managed
#
This server is managed by the data team.
Questions: ops@example.com
  • Roles are found in roles/ next to the playbook, then in roles_path (see ansible.cfg), then in installed collections.
  • Tasks in a role show up as motd : Write the message of the day, so you always know which role a task came from.
  • | comment turns ansible_managed into a properly commented line.
  • roles: entries run before the play's tasks: (after pre_tasks:). Use include_role/import_role when the role must run at a specific point between tasks.

What makes a good role

Do Avoid
one concern per role (nginx, firewall, linkly_app) a server role that does everything
every tunable in defaults/main.yml, prefixed with the role name variables without a prefix; settings hard-coded in tasks
idempotent tasks, changed_when on commands shell everywhere
handlers for restarts and reloads restarting in tasks
a README with the variables and an example play undocumented inputs
tests with Molecule "it worked on my VM"

Test yourself

Where do you put settings that users of your role are expected to override?

  1. vars/main.yml
  2. defaults/main.yml
  3. meta/main.yml
  4. tasks/main.yml

In a play with roles: and tasks:, which runs first?

  1. tasks:
  2. roles:
  3. They are interleaved
  4. Alphabetical order

Which command creates a role skeleton?

  1. ansible-galaxy role init NAME
  2. ansible-playbook --init
  3. molecule role new
  4. ansible-doc -t role