# Identify if host being processed is the first in any the groups being processed

**URL:** <https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655>\
**Category:** Ansible Project\
**Created:** [October 2, 2017, 9:06am UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655 "2017-10-02T09:06:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_L](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Marc\_L](https://forum.ansible.com/u/Marc_L)\
**Post date:** [October 2, 2017, 9:06am UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655/1 "2017-10-02T09:06:49Z")

</div>

I have some tasks that should only be processed if the host (inventory\_hostname) is the first in the group of servers it’s defined in.

For example, here is my hosts file. Note that the group names are not fixed - the Role I want to use them in cannot rely on the name of the groups - it needs to be a bit more dynamic than that:

**Hosts**

`  
server1  
server2  
server3

[dev\_cluster]  
server1  
server2  
server3

[frontend\_nodes]  
server1  
server2

[frontend\_nodes:vars]  
node\_type\_prefix=  
node\_port=19043  
node\_protocol=https

[backend\_nodes]  
server3

[backend\_nodes:vars]  
node\_type\_prefix=backend/  
node\_port=19043  
node\_protocol=https  
`

I have a playbook which I call like so, by passing in the name of the two groups - again - the name of these groups could vary, or I could add a 3rd or 4th group in.

Playbook

`

---

<div class="post-metadata">

**Author:** ![Matthew\_Wyant1](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Matthew\_Wyant1](https://forum.ansible.com/u/Matthew_Wyant1)\
**Post date:** [October 2, 2017, 10:55am UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655/2 "2017-10-02T10:55:18Z")

</div>

Yes, there is a “groups” variable which is a dictionary with group name keys where the values are a list of hosts in that group. That is, groups[dev\_cluster] = [“server1”, “server2”, “server3”]. So you can do a inventory\_hostname == groups[dev\_cluster][0] comparison for the first item.

Note: I have not tested this in playbooks, only templates, and you might run into bugs if your group has zero hosts. You also need to consider the scenario where a host is in more than one group.

---

<div class="post-metadata">

**Author:** ![Marc\_L](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Marc\_L](https://forum.ansible.com/u/Marc_L)\
**Post date:** [October 2, 2017, 3:15pm UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655/3 "2017-10-02T15:15:17Z")

</div>

Hi,

That is close, but that still needs the name “dev\_cluster” in the cpmparison function.  
I am more looking for something that determines if the inventory\_hostname is the first server in either of the groups specified in the play.  
The groups may change across environments, so the task needs to be dynamic in its evaluation.

Is there any way to pick up the “hosts” that were specified by the play?

---

<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:** [October 2, 2017, 5:36pm UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655/4 "2017-10-02T17:36:56Z")

</div>

You only get the expanded result, not the exact string.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 2, 2017, 5:47pm UTC](https://forum.ansible.com/t/identify-if-host-being-processed-is-the-first-in-any-the-groups-being-processed/25655/5 "2017-10-02T17:47:29Z")

</div>

something like this?

when: inventory\_hostname in (groups|map('first')|list)

though you might want to remove all and empty groups first
