Interview Prep/Ansible/Roles, Collections and Galaxy

Role B picks up `listen_port: 9000` from role A, even though role B has its own default

AdvancedTroubleshootingRoles, Collections and Galaxy
Answer

Likely causes → By default (private_role_vars = False), the vars/ and defaults/ of a role imported via roles: or import_role become visible to the rest of the play. Role A's vars/main.yml value outranks role B's defaults/.

Fix → Prefix every role variable with its role name (rolea_listen_port, roleb_listen_port). If that is not possible, set:

[defaults]
private_role_vars = True

or use include_role with public: false (the default for include_role).

Prevent → Enforce var-naming[no-role-prefix] in ansible-lint.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Galaxy