# Global variable to pas dynamic data between plays with different hosts

**URL:** <https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102>\
**Category:** Ansible Project\
**Created:** [November 25, 2016, 4:11pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102 "2016-11-25T16:11:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pieter\_Voet](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pieter\_Voet](https://forum.ansible.com/u/Pieter_Voet)\
**Post date:** [November 25, 2016, 4:11pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/1 "2016-11-25T16:11:18Z")

</div>

Hi all,

I’ve been looking into this for some time now, and I think I definitely can use some help here.  
What I want is to set a variable from a task specific to one host from a group, with a value that is set dynamically by the task outcome.  
Then next, I want to use this variable by another play, with more hosts from that same group.

e.g. :

group = { host\_a, host\_b, host\_c }

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 29, 2016, 5:48pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/2 "2016-11-29T17:48:22Z")

</div>

Why can't do that?  
You can in the second play also access the result variable directly like so  
{{ hostvars['host\_a']['result'] }}

---

<div class="post-metadata">

**Author:** ![Pieter\_Voet](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pieter\_Voet](https://forum.ansible.com/u/Pieter_Voet)\
**Post date:** [November 30, 2016, 9:08am UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/3 "2016-11-30T09:08:29Z")

</div>

True. And thanks for contributing.  
I knew this. I think I oversimplified my testcase… Thing is, our inventory has many groups, where hosts can be member  
of multiple groups… Here’s a modified testcase :

> groupall = { host\_a, host\_b, host\_c }

group1 = { host\_a }  
group2 = { host\_b, host\_c }

> * * *
> 
> - hosts: group1  
> tasks:
> 
> - name: get the variable value  
> whatever\_module: arg=arg\_a  
> register: result
> 
> - hosts: group2  
> tasks:
> 
> - debug: var=“{{result}}”
> 
> The playbook is started with the ‘–limit groupall’ argument.

Now, because of the groups, the second play cannot lookup ‘host\_a’ in the hostvars, since it has no knowledge  
about the hosts in ‘group1’…

Any ideas ?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 30, 2016, 10:42am UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/4 "2016-11-30T10:42:23Z")

</div>

Knowledge of members in group1 you find with groups['group1']

So this should work.

hostvars[groups['group1'][0]]['result']

---

<div class="post-metadata">

**Author:** ![Pieter\_Voet](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pieter\_Voet](https://forum.ansible.com/u/Pieter_Voet)\
**Post date:** [November 30, 2016, 10:50am UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/5 "2016-11-30T10:50:16Z")

</div>

Hi Kai,

you said :

> Knowledge of members in group1 you find with groups[‘group1’]  
> hostvars[groups[‘group1’][0]][‘result’]

actually, the ‘group1’ group contains a lot of hosts… There is no quarantee  
that the first entry in that array actually is the host that applies to my playbook.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 30, 2016, 12:24pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/6 "2016-11-30T12:24:27Z")

</div>

Then I don't understand what you are trying to achieve, your testcase must be flawed.

Every host in group1 will run that task, so every host in the group will have a "result" variable set.  
You must choose at some level who is going to be the "global" one.

---

<div class="post-metadata">

**Author:** ![Pieter\_Voet](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pieter\_Voet](https://forum.ansible.com/u/Pieter_Voet)\
**Post date:** [November 30, 2016, 12:51pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/7 "2016-11-30T12:51:49Z")

</div>

OK, redefining my testcase then :

> groupall = { host\_a, host\_b, host\_c }

group1 = { host\_x, host\_y, host\_a, host\_z }  
group2 = { host\_b, host\_c }

> * * *
> 
> - hosts: group1
> 
> tasks:
> 
> - name: get the variable value  
> whatever\_module: arg=arg\_a  
> register: result
> 
> - hosts: group2
> 
> tasks:
> 
> - debug: var=“{{result}}”
> 
> The playbook is started with the ‘–limit groupall’ argument.

Yes, every host in ‘group1’ will have access to the result.  
What I want is to access that result in the other play ( for ‘group2’ ).  
( BTW, I understand that the first play will only be executed on ‘host\_a’ since that’s the only host that is in ‘groupall’…)

For now I’ll call it a playbook ‘global’ variable… Like an ‘extra\_var’ , but then settable from within a play.

Thanks !

---

<div class="post-metadata">

**Author:** ![Pieter\_Voet](https://avatars.discourse-cdn.com/v4/letter/p/5fc32e/32.png) [@Pieter\_Voet](https://forum.ansible.com/u/Pieter_Voet)\
**Post date:** [December 5, 2016, 12:12pm UTC](https://forum.ansible.com/t/global-variable-to-pas-dynamic-data-between-plays-with-different-hosts/23102/8 "2016-12-05T12:12:21Z")

</div>

OK, so I looked at Ansible’s code, and I now know how to implement this. I tried my modification locally and  
it works fine. It basically adds a module that adds an ‘extra\_vars’ variable if it does not yet exist.  
That way, any variable set on the commandline using the ‘–extra-vars’ argument cannot be overruled by the  
playbook.

Now this scenario works :

Inventory :  
group\_all = { host\_1, host\_2, host\_3 }  
group\_one = { host\_1 }  
group\_two = { host\_2, host3 }

Playbook ‘my\_playbook.yml’ :
