From within a role, call another role and pass variables at runtime?

Hello -

Can Ansible call a role from within another role, as if it’s calling a function? Could that other role be passed certain variables at runtime?

I have a few tasks that are repeated several times in my plays. Right now, I just copy/paste the same task and tweak an object or two as needed. However, if I want to change something about that repeated task I then have to find all the places I put it in order for it to change. Also, the repeated tasks pollute the readability of the task yaml. It lacks modularity.

I dropped some sample code at the end. The yellow items pull the md5 of two files for comparison, so I can take certain actions if they don’t match. The blue items are logged results, wherein I may want to tweak the content later to match changing demands. Could I split out these functions into their own roles (with passed variables) and call them within a certain play? I could then have one task with pseudo code “Call role md5 compare(filepath, filename)” and “Call role md5 logging(copy_output)”.

I’m new to Ansible and still getting my feet wet. Open to any suggestions. Thank you! :slight_smile:

Sample Code:

roles before 2.2 only had dependencies, but in that version we
introduced 'include_role' that can be placed as a task.