Interview Prep/Ansible/Roles, Collections and Galaxy

Half the tasks of an imported role are skipped after the first task runs

AdvancedTroubleshootingRoles, Collections and Galaxy
Answer

Likely causes → when on import_role or import_tasks is copied onto every task inside, and evaluated again for each one. If the role sets the variable used in the condition (for example when: not app_installed, and a task sets app_installed: true), the remaining tasks are skipped.

Fix → Use a dynamic include so the condition is evaluated once:

- ansible.builtin.include_role:
    name: app
  when: not app_installed

Or use a condition variable that the role does not change.

Prevent → Do not let a role change the variables that gate it. Remember: imports copy the condition onto each task, includes evaluate it once.

All Ansible questions Roadmap: Ansible

More on Roles, Collections and Galaxy