Interview Prep/Ansible/Roles, Collections and Galaxy

How do tags and `when` apply to roles?

IntermediateConceptRoles, Collections and Galaxy
Answer

With the roles: keyword, tags and when are applied to every task in the role. With include_role, they apply only to the include task itself unless you wrap with apply.

roles:
  - { role: nginx, tags: [web], when: "'web' in group_names" }

tasks:
  - ansible.builtin.include_role:
      name: nginx
      apply:
        tags: [web]
    tags: [web]

The second form needs the tag on both the include and in apply, otherwise --tags web will skip the include and the inner tasks never run.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Galaxy