How to get values for inventory files using with_inventory_hostname

Hi all,

I am trying to figure out how to use inventory created dynamically in my playbook.

I dynamically create an inventory file as below and place it in inventory folder :

[gapp]
gapp[01:02]-leo.dev.com role=gapp
[papp]
papp[01:02]-leo.dev.com role=papp
[relay]
relay[01:02]-leo.dev.com role=relay
[web]
web[01:02]-leo.dev.com role=web

[leo:children]
relay
web
papp
gapp

now i am trying retrieve each of the host along with the role attributes.

  • debug: var=“{{ item }}”
    with_inventory_hostnames: “{{env}}”

The output returns me the various hostnames like :
web01-leo.dev.com
web02-leo.dev.com


How do i get the role attribute from the host file? The reason i am using the with_inventory_hostname so that i could create a pattern for instance as web[01-10] rather than individual servers.

Thanks in advance,
Pawan Venugopal

Normally this gives you the attribute 'some_attribute' for a hostname
'foobar':
hostvars['foobar']['some_attribute']

So, in your case, I would try:
- debug: var="{{ hostvars[item]['some_attribute'] }}"
  with_inventory_hostnames: "{{env}}"

Johannes