# accessing variable defined in a role from another role not working

**URL:** <https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751>\
**Category:** Ansible Project\
**Created:** [July 11, 2019, 6:47am UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751 "2019-07-11T06:47:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shashank\_Dutt\_Jha](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Shashank\_Dutt\_Jha](https://forum.ansible.com/u/Shashank_Dutt_Jha)\
**Post date:** [July 11, 2019, 6:47am UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751/1 "2019-07-11T06:47:52Z")

</div>

I have defined a role : say A  
another role B which includes role A

in role B when I try to use variables defined in A, I get var undefined error.

I tried using public: yes when including role A. Still same issue.

This is correct behaviour?

Or I should be able to use variables defined in a role in my tasks/ role tasks after including it?

---

<div class="post-metadata">

**Author:** ![Andrew\_Feller](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Andrew\_Feller](https://forum.ansible.com/u/Andrew_Feller)\
**Post date:** [July 11, 2019, 9:41am UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751/2 "2019-07-11T09:41:06Z")

</div>

This behavior is expected.

The variables defined in Role A should be scoped for A unless defined within your inventory or any of the other places outside the role that variables are defined.

If variables from a role were implicitly available broadly through their declaration, you can have nasty side effects and potential security problems.

Recommend looking at [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html) for more info.

---

<div class="post-metadata">

**Author:** ![Shashank\_Dutt\_Jha](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Shashank\_Dutt\_Jha](https://forum.ansible.com/u/Shashank_Dutt_Jha)\
**Post date:** [July 11, 2019, 10:36am UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751/3 "2019-07-11T10:36:14Z")

</div>

Yeah, I will assume so.

But the confusing was [https://docs.ansible.com/ansible/latest/modules/include\_role\_module.html](https://docs.ansible.com/ansible/latest/modules/include_role_module.html)

The public option while including role that is described as  
This option dictates whether the role’s `vars` and `defaults` are exposed to the playbook. If set to `yes` the variables will be available to tasks following the `include_role` task. This functionality differs from standard variable exposure for roles listed under the `roles`header or `import_role` as they are exposed at playbook parsing time, and available to earlier roles and tasks as well.

---

<div class="post-metadata">

**Author:** ![Andrew\_Feller](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Andrew\_Feller](https://forum.ansible.com/u/Andrew_Feller)\
**Post date:** [July 11, 2019, 2:20pm UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751/4 "2019-07-11T14:20:40Z")

</div>

Ah, so the documentation in this regard might be very literal where the role variables are only available to the playbook and not the role importing the role:

> This option dictates whether the role’s `vars` and `defaults` are exposed to the playbook. If set to `yes` the variables will be available to tasks following the `include_role` task. This functionality differs from standard variable exposure for roles listed under the `roles` header or `import_role` as they are exposed at playbook parsing time, and available to earlier roles and tasks as well.

You might look [more at source code with the variable manager](https://github.com/ansible/ansible/blob/devel/lib/ansible/vars/manager.py#L196) to see where your issue might be as I wasn’t considering this particular case in your initial question; I thought you were talking about a [role listing another as a dependency via meta/main.yml](https://docs.ansible.com/ansible/latest/reference_appendices/galaxy.html#dependencies)

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [July 11, 2019, 2:23pm UTC](https://forum.ansible.com/t/accessing-variable-defined-in-a-role-from-another-role-not-working/30751/5 "2019-07-11T14:23:27Z")

</div>

role “vars” are scoped to the play, not the role. role “params”, which are different than vars, are scoped to the role. `include_role` and `import_role` do not support params, only vars.
