# ansible role meta, templated role dependency.

**URL:** <https://forum.ansible.com/t/ansible-role-meta-templated-role-dependency/29831>\
**Category:** Ansible Project\
**Created:** [March 6, 2019, 11:15am UTC](https://forum.ansible.com/t/ansible-role-meta-templated-role-dependency/29831 "2019-03-06T11:15:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [March 6, 2019, 11:15am UTC](https://forum.ansible.com/t/ansible-role-meta-templated-role-dependency/29831/1 "2019-03-06T11:15:10Z")

</div>

Hi all,

I have a bunch of nested roles for a specific project.

I’m trying to dynamically invoked roles based on variable I have in the inventory.  
For example, let’s say I have a foo variable that is defined for every hosts, the following will fail:

#roles/A/meta.yml

dependencies:

- { role: “B/{{foo}}” }

I would expect that it would load the roles in roles/B/, but rather I have the following error:

ERROR! ‘foo’ is undefined.

Strange things, If I invoke foo as a play variable like this:

- hosts: srv  
gather\_facts: True  
vars:  
foo: “bar”  
roles:
- { role: A }

It correctly loads and execute roles/B/bar…

So it seems to not expand inventory variable when passing them to “role” directive… Is this an expected behavior ? Is there any workaround ? This is kind of annoying because I wanted to dynamically load roles based on inventory variables rather than relying on conditional include using plenty of “when:” statement in the meta directive…

Best regards,

Lionel.

---

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [March 8, 2019, 7:12am UTC](https://forum.ansible.com/t/ansible-role-meta-templated-role-dependency/29831/2 "2019-03-08T07:12:51Z")

</div>

Anyone ?

---

<div class="post-metadata">

**Author:** ![Lionel\_H](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Lionel\_H](https://forum.ansible.com/u/Lionel_H)\
**Post date:** [March 8, 2019, 12:25pm UTC](https://forum.ansible.com/t/ansible-role-meta-templated-role-dependency/29831/3 "2019-03-08T12:25:16Z")

</div>

Made some progress by looking at the code.

In: lib/ansible/playbook/role/definition.py

`if self._variable_manager:`  
all\_vars = self.\_variable\_manager.get\_vars(play=self.\_play)  
templar = Templar(loader=self.\_loader, variables=all\_vars)  
if templar.\_contains\_vars(role\_name):  
role\_name = templar.template(role\_name)

Looks like that the variables are expanded from the play context, not from the inventory which is not what I want in this case.  
I want variables from the inventory to be expanded.

Is there any way to extend that behavior to include inventory variable ?

Best regards,

Lionel.
