Passing variables from role to role

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

ping - any advice on passing variables from one role to another?

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!

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.

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?

Yes, those are stored in the variable cache globally, and should be accessible within the same play from any other task.

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?

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