# Working with hostvars across hosts of dynamic inventory

**URL:** <https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122>\
**Category:** Ansible Project\
**Created:** [August 27, 2015, 9:06am UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122 "2015-08-27T09:06:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [August 27, 2015, 9:06am UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/1 "2015-08-27T09:06:06Z")

</div>

Hello All,

I have been struggling for quite a while now sharing some registered variables during ec2 instances provisioning hosts: localhost with their configuration hosts: tag\_Name\_tagname of ec2.

Kindly have a look at this sample [http://pastie.org/10378851](http://pastie.org/10378851) . In hosts: tag\_MName\_myappdblivemaster , I need to set the hostname of all the instances created during - name: Launch myapp DB Instances Availability A and - name: Launch myapp DB Instances Availability B.

Because I have not found any way of using the variable myapp\_db\_ec2\_a and myapp\_db\_ec2\_b in the other hosts like tag\_MName\_myappdblivemaster in particular, I had to break the run, copy the hostnames from the console and paste in in playbook before this can be successful. When I try with hostvars[‘localhost’] , I get basically nothing to work with.

Is there any thing I am missing or it’s not possible?

Best Regards,

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [August 27, 2015, 10:07am UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/2 "2015-08-27T10:07:19Z")

</div>

Hi,

A registered variable persists only during the play, so maybe you could make them facts which persists across plays as below

- set\_fact:

registered\_var: “{{ registered\_var }}”

and then it shoudl be available via hostvars[‘localhost’]

- Benno

---

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [August 27, 2015, 1:15pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/3 "2015-08-27T13:15:48Z")

</div>

Hello Benno,

I have tried your suggestion with set\_fact but it didn’t work. [http://pastie.org/10379408](http://pastie.org/10379408) has both the playbook and it’s output

Best Regards,

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [August 27, 2015, 2:13pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/4 "2015-08-27T14:13:46Z")

</div>

Hi Joseph,

which version of ansible are you on ?

- Benno

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [August 27, 2015, 2:30pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/5 "2015-08-27T14:30:56Z")

</div>

just checked the code, registered variable shoudl also persist across plays… please do let us know the verison of ansible and i can check.

- Benno

---

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [August 27, 2015, 2:33pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/6 "2015-08-27T14:33:16Z")

</div>

Hello Benno,

I am using ansible 1.9.2

Thanks

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [August 27, 2015, 2:41pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/7 "2015-08-27T14:41:58Z")

</div>

can you please try hostvars[‘127.0.0.1’]

---

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [August 27, 2015, 3:00pm UTC](https://forum.ansible.com/t/working-with-hostvars-across-hosts-of-dynamic-inventory/19122/8 "2015-08-27T15:00:48Z")

</div>

Hello Benno,

Totally awesome, loopback worked. thanks a million

Best Regards,
