# How to Pick and match the inventory\_hostname against {{ ansible\_hostname }} using groups.hosts

**URL:** <https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967>\
**Category:** Ansible Project\
**Created:** [March 12, 2018, 3:25pm UTC](https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967 "2018-03-12T15:25:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rraka1002](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rraka1002](https://forum.ansible.com/u/rraka1002)\
**Post date:** [March 12, 2018, 3:25pm UTC](https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967/1 "2018-03-12T15:25:11Z")

</div>

Hi Experts,

---

<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:** [March 12, 2018, 3:31pm UTC](https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967/2 "2018-03-12T15:31:53Z")

</div>

First of all you are comparing a string (inventory\_hostname) to a list  
(groups.hosts, which is [host-test1, host-test2, noi-pranjala]).

if you want to match inventory-hostname to ansible\_hostname, just do that:

when: inventory\_hostname == ansible\_hostname

that will skip the task when they don't match.

if you want the task to actually fail:

failed\_when: inventory\_hostname != ansiblie\_hostname

---

<div class="post-metadata">

**Author:** ![rraka1002](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rraka1002](https://forum.ansible.com/u/rraka1002)\
**Post date:** [March 12, 2018, 4:01pm UTC](https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967/3 "2018-03-12T16:01:39Z")

</div>

@Brian , Thanks a lot for your suggestion, this looks Great,

What about “when: inventory\_hostname == groups.hosts” when: inventory\_hostname == groups.hosts[0] , i google around but did not get clear & explicit explanation about its usage

---

<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:** [March 12, 2018, 4:11pm UTC](https://forum.ansible.com/t/how-to-pick-and-match-the-inventory-hostname-against-ansible-hostname-using-groups-hosts/26967/4 "2018-03-12T16:11:15Z")

</div>

This is invalid, as i explained above:  
when: inventory\_hostname == groups.hosts"

This works when the 'current host' is also the 'first host in the  
group', here [0] is an index on the list that is group.hosts, so it is  
a valid comparison.

when: inventory\_hostname == groups.hosts[0]
