Answers: <p>In lack of a better idea I chose this rather long approach:</p>
  
  <p><strong>myid.j2</strong></p>
  
  <pre><code>{% for host in groups['zookeeper'] %}
  {% if host == inventory_hostname %}
  {{ loop.index }}
  {% endif %}
  {% endfor %}
  </code></pre>
  
  <p>The idea is to iterate over all hosts and check if the current host being processed matches the host in the list of hosts. If it matches I write the iteration counter to the file.</p>
  
  <p>When I ssh into the boxes I can see their myid files were created according to my need:</p>
  
  <pre><code>[root@quorumandmonitoring data]# cat myid
  5
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-05-01T23:45:15.773Z
Answers: So far this the only way I'm able to do.
Answers: Shobit Mahajan
Answers: CC BY-SA 4.0
Answers: 2024-05-01T23:45:30.070Z
Answers: Have you found a better way to do this?
Answers: Shobit Mahajan
Answers: CC BY-SA 4.0
Answers: 2019-01-01T19:52:17.997Z
Answers: 2019-01-01T19:52:17.997Z
Answers: Marged
Body: <p>I'm trying to configure a Zookeeper Cluster with Ansible. As you might know Zookeeper has two important config files.</p>
  
  <p><strong>zookeeper/conf/zoo.cfg</strong></p>
  
  <p>I create this file on each cluster member using this jinja2 template:</p>
  
  <pre><code>{% for host in groups['zookeeper'] %}
  server.{{ loop.index }}={{ host }}:2888:3888
  {% endfor %}
  </code></pre>
  
  <p>The result will look like this:</p>
  
  <pre><code>server.1=processing1.srv.mycompany
  server.2=processing2.srv.mycompany
  server.3=indexing1.srv.mycompany
  server.4=indexing2.srv.mycompany
  server.5=quorumandmonitoring.srv.mycompany
  </code></pre>
  
  <p><strong>data/myid</strong></p>
  
  <p>Accordind to the <a href="http://zookeeper.apache.org/doc/r3.3.3/zookeeperAdmin.html#sc_zkMulitServerSetup" rel="nofollow noreferrer">docs</a> this file needs to contain an unique id for each member of the cluster.
  To keep everything in sync I would like to have the value after <code>server.</code> in the <code>myid</code> of each host. This means:</p>
  
  <ul>
  <li><code>myid=1</code> on <code>processing1</code></li>
  <li><code>myid=2</code> on <code>processing2</code></li>
  <li><code>myid=3</code> on <code>indexing1</code></li>
  <li><code>myid=4</code> on <code>indexing2</code></li>
  <li><code>myid=5</code> on <code>quorumandmonitoring</code></li>
  </ul>
  
  <p>And this is where my problem starts: how can I do this in a clever way? An ideal solution would be to use something like the <code>for host in groups</code> loop. I would rather not add <code>host_vars</code> to my inventory.</p>
  
Tags: <ansible><jinja><templating><zookeeper>
Title: Configuring Zookeeper from Ansible
UserDisplayName: Marged
ThreadText: # Configuring Zookeeper from Ansible
  
  (Asked by: username_0 on 2018-12-29)
  
  I'm trying to configure a Zookeeper Cluster with Ansible. As you might know Zookeeper has two important config files.
  
  **zookeeper/conf/zoo.cfg**
  
  I create this file on each cluster member using this jinja2 template:
  
  ```
  {% for host in groups['zookeeper'] %}
  server.{{ loop.index }}={{ host }}:2888:3888
  {% endfor %}
  
  ```
  
  The result will look like this:
  
  ```
  server.1=processing1.srv.mycompany
  server.2=processing2.srv.mycompany
  server.3=indexing1.srv.mycompany
  server.4=indexing2.srv.mycompany
  server.5=quorumandmonitoring.srv.mycompany
  
  ```
  
  **data/myid**
  
  Accordind to the [docs](http://zookeeper.apache.org/doc/r3.3.3/zookeeperAdmin.html#sc_zkMulitServerSetup) this file needs to contain an unique id for each member of the cluster.
  To keep everything in sync I would like to have the value after `server.` in the `myid` of each host. This means:
  
  * `myid=1` on `processing1`
  * `myid=2` on `processing2`
  * `myid=3` on `indexing1`
  * `myid=4` on `indexing2`
  * `myid=5` on `quorumandmonitoring`
  
  And this is where my problem starts: how can I do this in a clever way? An ideal solution would be to use something like the `for host in groups` loop. I would rather not add `host_vars` to my inventory.
  
  **Tags:** ansible, jinja, templating, zookeeper
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-01-01. Score: 2
  
  In lack of a better idea I chose this rather long approach:
  
  **myid.j2**
  
  ```
  {% for host in groups['zookeeper'] %}
  {% if host == inventory_hostname %}
  {{ loop.index }}
  {% endif %}
  {% endfor %}
  
  ```
  
  The idea is to iterate over all hosts and check if the current host being processed matches the host in the list of hosts. If it matches I write the iteration counter to the file.
  
  When I ssh into the boxes I can see their myid files were created according to my need:
  
  ```
  [root@quorumandmonitoring data]# cat myid
  5
  
  ```
  
  > **Comments:**
  > **username_1**: So far this the only way I'm able to do.  
  > **username_1**: Have you found a better way to do this?  
  
  ---
  
  