# What does the \[0\] mean in   when: inventory\_hostname ==  groups\['mongo\_servers'\]\[0\]  ?

**URL:** <https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039>\
**Category:** Ansible Project\
**Created:** [December 1, 2013, 6:50am UTC](https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039 "2013-12-01T06:50:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Coronel](https://avatars.discourse-cdn.com/v4/letter/d/96bed5/32.png) [@David\_Coronel](https://forum.ansible.com/u/David_Coronel)\
**Post date:** [December 1, 2013, 6:50am UTC](https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039/1 "2013-12-01T06:50:51Z")

</div>

I saw this in the ansible-examples / openshift / openshift\_ec2 / roles / mongodb / tasks / main.yml

- name: Initialize the replication set  
shell: /usr/bin/mongo --port “{{ mongod\_port }}” /tmp/repset\_init.js;touch /opt/rep.init creates=/opt/rep.init  
when: inventory\_hostname == groups[‘mongo\_servers’][0]

What does the [0] mean in when: inventory\_hostname == groups[‘mongo\_servers’][0] ?

I wanted to find a conditional that would say: when inventory\_hostname is not in group mailservers

It seems to work for me with this now:

when: inventory\_hostname != groups[‘mailservers’][0]

But I just don’t understand what the syntax means.

Thanks!

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [December 2, 2013, 6:03am UTC](https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039/2 "2013-12-02T06:03:27Z")

</div>

Hi David,

That means the first host in the group “mongo\_Servers”. [0] notion means the first element in a list. similarly [1] means the second element in a list.

Regards,  
Benno

---

<div class="post-metadata">

**Author:** ![Jon\_Murillo](https://avatars.discourse-cdn.com/v4/letter/j/c5a1d2/32.png) [@Jon\_Murillo](https://forum.ansible.com/u/Jon_Murillo)\
**Post date:** [July 19, 2016, 9:28pm UTC](https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039/3 "2016-07-19T21:28:34Z")

</div>

How can I use this **when** function to work with the hosts in my inventory file? I had _when: inventory\_hostname == play\_hosts[0]_ but I notice that it only selects a random server. I want it to select all of the servers. Using a \* doesn’t work…

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 20, 2016, 4:18pm UTC](https://forum.ansible.com/t/what-does-the-0-mean-in-when-inventory-hostname-groups-mongo-servers-0/14039/4 "2016-07-20T16:18:24Z")

</div>

I don't understand why you want to do this, inventory\_hostname will always be in play\_hosts, so it will always be true.  
Maybe you are looking for "with\_items: play\_hosts"?

To check if something is in a list the syntax is  
when: \<variable\> in \<list\>
