# Share variables between roles running on different hosts

**URL:** <https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881>\
**Category:** Ansible Project\
**Created:** [March 31, 2015, 12:17pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881 "2015-03-31T12:17:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 31, 2015, 12:17pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/1 "2015-03-31T12:17:36Z")

</div>

Hello all,

I have a playbook that runs different roles on different hosts.  
Is it possible to pass a variable from one role running on one host to another role on another host running within the same playbook run? Or any workaround ?

playbook  
host1  
role1  
here I get some variables: var1 var2 …etc  
host2  
role2  
here I need to use var1 var2 … etc.

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [March 31, 2015, 12:58pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/2 "2015-03-31T12:58:47Z")

</div>

Looks like you're in need of some magic.

[http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

Giovanni

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 31, 2015, 2:13pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/3 "2015-03-31T14:13:49Z")

</div>

Hi,

I tried with hostvars:

{{ hostvars.LBL.db.stdout }};"  
{{ hostvars[‘LBL’][‘db’] }}  
{{ hostvars[‘LBL’][‘db’][‘stdout’] }}

but it says:

in get\_variables raise Exception(“host not found: %s” % hostname) Exception: host not found: LBL  
And LBL is defined, after all I’m running the first role on LBL and it’s ok.  
I’m using: ansible-1.8.4-1.el6.noarch

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [March 31, 2015, 2:56pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/4 "2015-03-31T14:56:23Z")

</div>

Working example:

---

<div class="post-metadata">

**Author:** ![Adrian\_Paraschiv](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@Adrian\_Paraschiv](https://forum.ansible.com/u/Adrian_Paraschiv)\
**Post date:** [March 31, 2015, 3:18pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/5 "2015-03-31T15:18:55Z")

</div>

I modified it to suit my hosts:

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [March 31, 2015, 4:11pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/6 "2015-03-31T16:11:30Z")

</div>

Tested on 1.8.4 and 1.9.0.1

Giovanni

---

<div class="post-metadata">

**Author:** ![Adam\_Herzog](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Adam\_Herzog](https://forum.ansible.com/u/Adam_Herzog)\
**Post date:** [March 31, 2015, 6:16pm UTC](https://forum.ansible.com/t/share-variables-between-roles-running-on-different-hosts/17881/7 "2015-03-31T18:16:33Z")

</div>

> I modified it to suit my hosts:
> 
> ---  
> - name: test hostvars host1  
> &nbsp;&nbsp;hosts: LBL  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- command: "ls /bin"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: ls\_out
> 
> - name: test hostvars host2  
> &nbsp;&nbsp;hosts: LM  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: "{{ hostvars['LBL']['ls\_out']['stdout'] }}"
> 
> and it doesn't work:

\<snip\>

> FATAL: all hosts have already failed -- aborting
> 
> the ls /bin is been executed but it's the same error  
> this is the content of my hosts file:
> 
> [LBL]  
> 10.104.148.136  
> [LM]  
> 10.104.148.138

LBL is the name of the group, not the host. You'd want something like

&nbsp;&nbsp;var: "{{ hostvars['10.104.148.136']['ls\_out']['stdout'] }}"

You can also get the list of hosts in a group, using {{ groups['LBL'] }}, if you'd rather just get the first host from there or if you want to iterate over the entire group.

There's more info and examples about this on the link posted earlier in this thread: [http://docs.ansible.com/playbooks\_variables.html#magic-variables-and-how-to-access-information-about-other-hosts](http://docs.ansible.com/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts)

-A
