{% for host in groups['zookeeper'] -%} {% if hostvars[host]['ansible_hostname'] == ansible_hostname -%} {{ loop.index }} {%- endif %} {%- endfor %}