# reference role default variable

**URL:** <https://forum.ansible.com/t/reference-role-default-variable/32493>\
**Category:** Ansible Project\
**Created:** [March 18, 2020, 12:53pm UTC](https://forum.ansible.com/t/reference-role-default-variable/32493 "2020-03-18T12:53:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Stroder](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Michael\_Stroder](https://forum.ansible.com/u/Michael_Stroder)\
**Post date:** [March 18, 2020, 12:53pm UTC](https://forum.ansible.com/t/reference-role-default-variable/32493/1 "2020-03-18T12:53:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [March 18, 2020, 12:55pm UTC](https://forum.ansible.com/t/reference-role-default-variable/32493/2 "2020-03-18T12:55:49Z")

</div>

Can you please explain your actual use case instead of describing your problem with bogus variable and role names?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 18, 2020, 1:39pm UTC](https://forum.ansible.com/t/reference-role-default-variable/32493/3 "2020-03-18T13:39:00Z")

</div>

> 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)

&nbsp;&nbsp;\> cat roles/role\_b/defaults/main.yml  
&nbsp;&nbsp;var\_b: "{{ ('role\_a' in ansible\_role\_names)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ternary(var\_a, 'var\_b\_default\_in\_role\_b') }}"

> Can I reference role vars?

Yes. Simply reference the variable. For example

&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](https://docs.ansible.com/ansible/latest/modules/include_role_module.html#parameter-public)

HTH,

&nbsp;&nbsp;-vlado
