Passing variables from role to role

2,418 views
Skip to first unread message

Ananda Debnath

unread,
Nov 25, 2014, 9:58:49 AM11/25/14
to ansible...@googlegroups.com
Hola,

I have a playbook with multiple roles mapped to multiple vms. I'm registering a variable in the output of one of the tasks in role A and need to pass it to role B. Calling register or set_facts in the role A tasks file works as expected within role A, but this var doesn't seem to make it into the tasks listed in role B. I finally used hostvars['vmA']['myvar'] to access this variable from the tasks of role B. So I suspect I'm missing something about how vars are scoped and referenced.

Is hostvars the recommended way to do this or am I missing something obvious?

Thanks,
Ananda

Ananda Debnath

unread,
Dec 1, 2014, 2:41:07 PM12/1/14
to ansible...@googlegroups.com
*ping* - any advice on passing variables from one role to another?

James Cammarata

unread,
Dec 1, 2014, 5:32:15 PM12/1/14
to ansible...@googlegroups.com
Hi Ananda, 

Registered variables are available in the global scope and can be accessed by any roles within the same play. However, I would recommend against this as it could make your roles very fragile and very tightly coupled, meaning their reusability would be limited. It sounds like this functionality should all be in a single role rather than split into multiples.

Hope that helps!

--
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/7c26bcc7-4324-4549-a2b2-ba1f6825e9c5%40googlegroups.com.

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

Ananda Debnath

unread,
Dec 2, 2014, 12:56:45 AM12/2/14
to ansible...@googlegroups.com
Thanks James.

I understand the recommendation - I'll look into decoupling one of these days. For now though, I have a hard dependency - inventory-vm-A/role-A is generating a signed cloudfront url (using aws creds that exist in vm-A) that needs to be downloaded by inventory-vm-B/role-B without needing to ship aws credentials to vm-B - and all of it needs to run in the same play.

I can't seem to be able to access registered variables directly though - if I call debug on the registered variable carrying the cloudfront URL within any other task item in role-A, it works fine. Calling debug on the same var in role-B (which is running on inventory vm-B) in the same play (i.e. listed sequentially within the same site.yml file) - shows a blank value. The only way I've been able to access it is by using hostvars[vm-A][reg-var-name]. Simply using "{{ reg-var-name }}" only works in role-A tasks and not role-B tasks.

I'm using Ansible 1.7.2 on Ubuntu 14.04.

Ananda Debnath

unread,
Dec 2, 2014, 11:40:02 PM12/2/14
to ansible...@googlegroups.com
From what I'm hearing I should be able to directly access registered vars/set_fact facts onwards from where I declare and define it regardless of the play I'm in... correct?

James Cammarata

unread,
Dec 3, 2014, 12:29:06 AM12/3/14
to ansible...@googlegroups.com
Yes, those are stored in the variable cache globally, and should be accessible within the same play from any other task.

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

Ananda Debnath

unread,
Dec 3, 2014, 1:31:52 AM12/3/14
to ansible...@googlegroups.com
Thanks - I'm somewhat confused about the terminology now. I have a single playbook with multiple roles. Is this all considered the same play? It's certainly all executed within a single run of ansible-playbook.

I can access the registered vars from the same role in the playbook, but not from another role in the same playbook without using the hostvars scoped names. Is this expected?

Brian Coca

unread,
Dec 3, 2014, 7:51:21 AM12/3/14
to ansible...@googlegroups.com
a play is a host to task mapping, roles are a group of tasks, as long
as they are in the same hosts mapping they are in the same play:

- hosts: all
roles:
- role1
- role2

you can have additional plays in the same file or execute several
files in the same invocation, both of these are named playbooks
(contains plays, at least 1).


--
Brian Coca
Reply all
Reply to author
Forward
0 new messages