I have a piece of code which is required to run by many roles.
Instead of duplicating these tasks which brings maintenance problem as it grows, I would like somehow to re-use same set of tasks across many roles and define them just in one place.
for ease of use I would store it in the same directory as your other playbooks.
If you have a bigger / more complex setup I would recommend storing all the “shared” ymls in one location.
Then make the path to those yml files a variable so you can move it around later without having to edit every include task manually.
I extensively use roles and now came to idea that other roles share some tasks in common.
What I’ve done so far I’ve split the initial main.yml task file into pieces and use include to re-use these tasks from other roles.
May be not an elegant solution but it works.
You can include role dependencies in the meta/main.yml for a role. These are roles that will also be installed by the role, prior to the role installing itself.
So, for common things you need on all machines, you can have a ‘common’ role. Then, in the meta/main.yml for more specific roles, you can establish ‘common’ as a dependency. This is a snippet from one of my more complex roles that combines a lot of smaller roles: