reference role default variable

12 views
Skip to first unread message

Michael Ströder

unread,
Mar 18, 2020, 8:53:27 AM3/18/20
to Ansible Project
HI!

I'd like to override a default for

var_b defined in roles/role_b/defaults/main.yml

with a value of

var_a defined in roles/role_a/defaults/main.yml

Is this possible?
Can I reference role vars?
Or is this rather a use-case of include_role?

Ciao, Michael.

Stefan Hornburg (Racke)

unread,
Mar 18, 2020, 8:56:14 AM3/18/20
to ansible...@googlegroups.com
Can you please explain your actual use case instead of describing your problem with bogus variable and role names?

Regards
Racke

--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

Vladimir Botka

unread,
Mar 18, 2020, 9:39:27 AM3/18/20
to Michael Ströder, ansible...@googlegroups.com
On Wed, 18 Mar 2020 13:53:09 +0100
Michael Ströder <mic...@stroeder.com> wrote:

> I'd like to override a default for
> var_b defined in roles/role_b/defaults/main.yml
>
> with a value of
>
> var_a defined in roles/role_a/defaults/main.yml
>
> Is this possible?

If you import the roles try (not tested)

shell> cat roles/role_b/defaults/main.yml
var_b: "{{ ('role_a' in ansible_role_names)|
ternary(var_a, 'var_b_default_in_role_b') }}"

> Can I reference role vars?

Yes. Simply reference the variable. For example

- debug:
var: var_b

> Or is this rather a use-case of include_role?

Include role is more complex because the variables of the included role might
be available in the playbook after the include. See attribute "public"
https://docs.ansible.com/ansible/latest/modules/include_role_module.html#parameter-public

HTH,

-vlado
Reply all
Reply to author
Forward
0 new messages