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

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:

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
  action: apt pkg=zookeeperd

- name: write zookeeper's myid
  action: template src=../templates/myid.j2 dest=/var/lib/zookeeper/myid
  notify:
    - restart zookeeper

- name: write zookeeper' zoo.cfg
  action: template src=../templates/zoo.cfg.j2
dest=/etc/zookeeper/conf/zoo.cfg backup=yes
  notify:
    - 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
and then use {{ hostvars[host]['myid'] }}

Daniel

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.

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

Thanks to Daniel's note, that works great.

To flush out the docs at 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.

   groups
   hostvars
   ansible_managed
   template_host
   template_uid
   template_path
   template_fullpath
   template_run_date

Blair

$ansible_hostname
$ansible_hostname_short (does not have FQDN if there was one on the above)

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

General concept correct, actual letters not so much :slight_smile: