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

**URL:** <https://forum.ansible.com/t/from-within-a-role-call-another-role-and-pass-variables-at-runtime/22968>\
**Category:** Ansible Project\
**Created:** [November 5, 2016, 11:41pm UTC](https://forum.ansible.com/t/from-within-a-role-call-another-role-and-pass-variables-at-runtime/22968 "2016-11-05T23:41:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 5, 2016, 11:41pm UTC](https://forum.ansible.com/t/from-within-a-role-call-another-role-and-pass-variables-at-runtime/22968/1 "2016-11-05T23:41:12Z")

</div>

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! 🙂

Sample Code:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 10, 2017, 3:37am UTC](https://forum.ansible.com/t/from-within-a-role-call-another-role-and-pass-variables-at-runtime/22968/2 "2017-02-10T03:37:21Z")

</div>

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