accessing variable defined in a role from another role not working

28 views
Skip to first unread message

Shashank Dutt Jha

unread,
Jul 11, 2019, 2:47:52 AM7/11/19
to Ansible Project
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?

Andrew Feller

unread,
Jul 11, 2019, 5:41:26 AM7/11/19
to ansible...@googlegroups.com
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 for more info.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/705f9907-6f37-49f6-9372-aad55d12fc4c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Shashank Dutt Jha

unread,
Jul 11, 2019, 6:36:15 AM7/11/19
to Ansible Project
Yeah, I will assume so.


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 rolesheader or import_role as they are exposed at playbook parsing time, and available to earlier roles and tasks as well.



On Thursday, 11 July 2019 15:11:26 UTC+5:30, Andrew Feller wrote:
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 for more info.

Sent from my iPhone

On Jul 11, 2019, at 2:47 AM, Shashank Dutt Jha <shash...@gmail.com> wrote:

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?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Andrew Feller

unread,
Jul 11, 2019, 10:21:02 AM7/11/19
to ansible...@googlegroups.com
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 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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Matt Martz

unread,
Jul 11, 2019, 10:24:18 AM7/11/19
to ansible...@googlegroups.com
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.


For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net
Reply all
Reply to author
Forward
0 new messages