Configuring Zookeeper from Ansible
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