# Other host's host variables not available (for Zookeeper)

**URL:** <https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253>\
**Category:** Ansible Project\
**Created:** [January 26, 2013, 8:59pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253 "2013-01-26T20:59:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [January 26, 2013, 8:59pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/1 "2013-01-26T20:59:40Z")

</div>

I’m trying to build a playbook that will deploy and configure a Zookeeper cluster. For a cluster, Zookeeper requires that “You attribute the server id to each machine by creating a file named myid” [1]. The zoo.cfg needs to have the IP address and myid for all the hosts in the cluster.

So I’ve got this for my host list:

[zookeepers]  
192.168.2.2 myid=1  
192.168.2.3 myid=2  
192.168.2.4 myid=3

My tasks:

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 26, 2013, 9:04pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/2 "2013-01-26T21:04:36Z")

</div>

Blair Zajac wrote:

> I'm trying to build a playbook that will deploy and configure a Zookeeper  
> cluster. For a cluster, Zookeeper requires that "You attribute the server  
> id to each machine by creating a file named myid" [1]. The zoo.cfg needs  
> to have the IP address and myid for all the hosts in the cluster.
> 
> So I've got this for my host list:
> 
> [zookeepers]  
> 192.168.2.2 myid=1  
> 192.168.2.3 myid=2  
> 192.168.2.4 myid=3
> 
> My tasks:
> 
> ---  
> - name: install zookeeperd  
> &nbsp;&nbsp;action: apt pkg=zookeeperd
> 
> - name: write zookeeper's myid  
> &nbsp;&nbsp;action: template src=../templates/myid.j2 dest=/var/lib/zookeeper/myid  
> &nbsp;&nbsp;notify:  
> &nbsp;&nbsp;&nbsp;&nbsp;- restart zookeeper
> 
> - name: write zookeeper' zoo.cfg  
> &nbsp;&nbsp;action: template src=../templates/zoo.cfg.j2  
> dest=/etc/zookeeper/conf/zoo.cfg backup=yes  
> &nbsp;&nbsp;notify:  
> &nbsp;&nbsp;&nbsp;&nbsp;- restart zookeeper
> 
> Here is myid.j2:  
> {{ myid }}
> 
> Here is zoo.cfg
> 
> # specify all zookeeper servers  
> # The fist port is used by followers to connect to the leader  
> # The second one is used for leader election  
> {% for host in groups['zookeepers'] %}  
> # server.{{ HOW TO GET myid HERE}}={{ host }}:2888:3888  
> {% endfor %}

This is exactly what hostvars is for. See  
[http://ansible.cc/docs/playbooks2.html#accessing-information-about-other-hosts](http://ansible.cc/docs/playbooks2.html#accessing-information-about-other-hosts)  
and then use {{ hostvars[host]['myid'] }}

Daniel

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 26, 2013, 9:05pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/3 "2013-01-26T21:05:19Z")

</div>

Have you tried, inside that loop, something like:

{{ hostvars[host]['myid'] }}

?

Note the hosts must have been talked to at least once previously in  
the ansible run for that to be loaded for facts, inventory variables  
should be immediately accessible.

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [January 26, 2013, 9:07pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/4 "2013-01-26T21:07:23Z")

</div>

Sweet, thanks for the quick reply, that works great!

I think the template doc section could be expanded to mention this, I may put together a patch for it.

Blair

[1] [http://ansible.cc/docs/modules.html#template](http://ansible.cc/docs/modules.html#template)

---

<div class="post-metadata">

**Author:** ![Blair\_Zajac](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@Blair\_Zajac](https://forum.ansible.com/u/Blair_Zajac)\
**Post date:** [January 26, 2013, 9:15pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/5 "2013-01-26T21:15:37Z")

</div>

Thanks to Daniel's note, that works great.

To flush out the docs at [http://ansible.cc/docs/modules.html#template](http://ansible.cc/docs/modules.html#template), besides the following keys, are there any others? I didn't find the other examples that showed this.

&nbsp;&nbsp;&nbsp;groups  
&nbsp;&nbsp;&nbsp;hostvars  
&nbsp;&nbsp;&nbsp;ansible\_managed  
&nbsp;&nbsp;&nbsp;template\_host  
&nbsp;&nbsp;&nbsp;template\_uid  
&nbsp;&nbsp;&nbsp;template\_path  
&nbsp;&nbsp;&nbsp;template\_fullpath  
&nbsp;&nbsp;&nbsp;template\_run\_date

Blair

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 26, 2013, 9:42pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/6 "2013-01-26T21:42:19Z")

</div>

$ansible\_hostname  
$ansible\_hostname\_short (does not have FQDN if there was one on the above)

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [January 26, 2013, 9:48pm UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/7 "2013-01-26T21:48:54Z")

</div>

Michael DeHaan wrote:

> $ansible\_hostname  
> $ansible\_hostname\_short (does not have FQDN if there was one on the  
> above)

No, those are facts from the setup module. inventory\_hostname and  
inventory\_hostname\_short are special though.

In addition, there is group\_names. These are all documented in the section  
I linked earlier.

The template\_\* vars should indeed be documented in the template module  
documentation, as they are only available for templates.

Daniel

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [January 27, 2013, 2:13am UTC](https://forum.ansible.com/t/other-hosts-host-variables-not-available-for-zookeeper/12253/8 "2013-01-27T02:13:26Z")

</div>

General concept correct, actual letters not so much 🙂
