# How to get values for inventory files using with\_inventory\_hostname

**URL:** <https://forum.ansible.com/t/how-to-get-values-for-inventory-files-using-with-inventory-hostname/21399>\
**Category:** Ansible Project\
**Created:** [May 11, 2016, 6:59am UTC](https://forum.ansible.com/t/how-to-get-values-for-inventory-files-using-with-inventory-hostname/21399 "2016-05-11T06:59:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pawan\_kumar3](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@pawan\_kumar3](https://forum.ansible.com/u/pawan_kumar3)\
**Post date:** [May 11, 2016, 6:59am UTC](https://forum.ansible.com/t/how-to-get-values-for-inventory-files-using-with-inventory-hostname/21399/1 "2016-05-11T06:59:43Z")

</div>

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](http://web01-leo.dev.com)  
[web02-leo.dev.com](http://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

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 15, 2016, 3:16pm UTC](https://forum.ansible.com/t/how-to-get-values-for-inventory-files-using-with-inventory-hostname/21399/2 "2016-05-15T15:16:38Z")

</div>

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'] }}"  
&nbsp;&nbsp;with\_inventory\_hostnames: "{{env}}"

Johannes
