ansible role meta, templated role dependency.

25 views
Skip to first unread message

Lionel H

unread,
Mar 6, 2019, 6:15:11 AM3/6/19
to Ansible Project
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/<what foo point here>, 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. 

Lionel H

unread,
Mar 8, 2019, 2:12:51 AM3/8/19
to Ansible Project
Anyone ?

Lionel H

unread,
Mar 8, 2019, 7:25:16 AM3/8/19
to Ansible Project
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.



Le mercredi 6 mars 2019 12:15:11 UTC+1, Lionel H a écrit :
Reply all
Reply to author
Forward
0 new messages