Role inclusion mechanics

Hi,

I’d like to make a feature request wrt to role dependencies. Currently, role dependencies must be defined via the dependencies section in meta/main.yaml and always execute prior to the tasks in the parent role. However, we are encountering a particular issue as follows:

We have RoleA which has a dependency defined on RoleB. However, we’d like to execute a task before RoleB is executed. We do not control RoleB, and therefore, cannot modify its tasks. With the current role dependency mechanics, there doesn’t seem to be a way to execute pre_tasks before the execution of RoleB, short of creating a dummy role C which is included before RoleB.

Therefore, I was wondering whether the role inclusion mechanics could be made similar across playbooks and roles. That is, allow roles to include child roles, just as a playbook includes dependent roles, with pre_tasks and post_tasks. The advantages would be a simpler learning curve, since the mechanisms are more consistent, and also, provide more flexibility in terms of how roles can be composed, especially when dealing with third party roles.

cheers,

/Nuwan

Hi Nuwan,

It sounds like rather than making RoleB a dep on RoleA, you should simply include them in order, with any other pre-tasks you need to execute in another role, as follows:

roles:

  • pre_tasks_role
  • RoleB
  • RoleA

Beyond that, I would be more inclined to add pre/post task support to roles rather than modifying the inclusion mechanism in any way. If all you’re executing is RoleA/RoleB, you could simply use the existing pre_tasks structure too, which will execute before any role tasks are run.

Hope that helps!

Hi James,

Thanks for the reply. However, my understanding is that the suggestion you made wrt to pre_tasks would only work if there was a playbook involved, which in our case, there isn’t. We only have two roles, and we want to publish both roles in Ansible Galaxy. RoleA’s meta/main.yaml looks as follows:

dependencies:

  • { role: roleB }

However, since Ansible always executes the dependencies first, there is no way for us to insert a “pre_task” before roleB is executed as I understand?

If so, the only other option is to create a dummy pre_tasks_role, and pollute Ansible Galaxy with it. This seems a bit wasteful, and inelegant. I suspect that more people will run into similar issues as they try to compose roles out of other roles?

cheers,
/Nuwan