Is there a way to access the host index in a group? I.e. want number in the hosts group am i?

15,685 views
Skip to first unread message

Rob White

unread,
Nov 4, 2015, 11:43:06 PM11/4/15
to Ansible Project
I'm sure this must've come up before but I couldn't find an answer.

I have a list of hosts in a group.  I want to write an index number on each of those hosts in a file.  E.g. the first host has a file that contains 1, the second host has a file that contains 2 etc.

Now I know I can access the hosts using the groups variable E.g. groups["mygroup"] but i can't find any reference to an index so the host can find out which number it is.

Example play

...

- name: Add instance to new host group

  add_host:

    name: "{{ item }}"

    group: zk

  with_items:

    - server1

    - server2

    - server3


- name: deploy to zk group

  hosts: zk

  tasks:

    - name: write a myid file based on the position in the group

      copy: 

        content: {{ some_magic_variable?????? }}

        dest: /zookeeper-data/myid



In the above server i would expect to end up with three servers each with a myid file that contains 0, 1 or 2


Anyone know if that magic variable exists?

Mirko Friedenhagen

unread,
Nov 5, 2015, 12:30:01 AM11/5/15
to ansible-project
Hello Rob,

maybe http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-a-list-with-an-index
might help.

Regards
Mirko
Regards Mirko
--
http://illegalstateexception.blogspot.com/
https://github.com/mfriedenhagen/ (http://osrc.dfm.io/mfriedenhagen)
https://bitbucket.org/mfriedenhagen/
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/c3dec262-b1b5-414f-88ba-c326d4ccd93e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Rob White

unread,
Nov 5, 2015, 12:37:38 AM11/5/15
to Ansible Project
Unfortunately not.  I'm not iterating over a list.  Ansible is just making it's way through a group of hosts so i need Ansible to tell me which host it is operating on.

Matt Martz

unread,
Nov 5, 2015, 7:26:48 AM11/5/15
to ansible...@googlegroups.com
A few ways potentially:

{{ play_hosts.index(inventory_hostname) }}

If you want to know host index within a play


Or:

{{ groups.somegroup.index(inventory_hostname) }}

For getting the index of a host within a group.


For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Rob White

unread,
Nov 6, 2015, 1:22:38 AM11/6/15
to Ansible Project
Awesome Matt!

I knew it had to exist somewhere.

Thanks for sharing.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Giovanni Gaglione

unread,
Sep 7, 2017, 4:22:52 PM9/7/17
to Ansible Project
Hi Matt,

This solution does not work well when a group has multiple hostname that are all the same.

E.g.

[my-hosts]
192.168.1.1
192.168.1.1
192.168.1.1
192.168.1.1

In this case, do you recommend a way to get the index of the current host?
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Giovanni Gaglione

unread,
Sep 7, 2017, 5:05:41 PM9/7/17
to Ansible Project
Reply all
Reply to author
Forward
0 new messages