How to Pick and match the inventory_hostname against {{ ansible_hostname }} using groups.hosts

Hi Experts,

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

@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

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]