# Getting attributes from with\_inventory\_hostnames

**URL:** <https://forum.ansible.com/t/getting-attributes-from-with-inventory-hostnames/21411>\
**Category:** Ansible Developer\
**Created:** [May 11, 2016, 3:34pm UTC](https://forum.ansible.com/t/getting-attributes-from-with-inventory-hostnames/21411 "2016-05-11T15:34:19Z")\
**Posts on this page:** 3\
**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, 3:34pm UTC](https://forum.ansible.com/t/getting-attributes-from-with-inventory-hostnames/21411/1 "2016-05-11T15:34:19Z")

</div>

Hi ,

I am generating an inventory file dynamically for each cluster using templates and store it in inventory folder. Below is how the inventory files looks.

**Filename: leo**  
`  
[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

[leo:children]  
relay  
web  
papp  
gapp

I then use the following task to output the value

`

- debug: msg=“{{ item }}”  
with\_inventory\_hostnames: “{{env}}”  
`

output:

`

TASK [provisioning : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> ([item=papp02-leo.dev.com](http://item=papp02-leo.dev.com)) =\> {  
“item”: “[papp02-leo.dev.com](http://papp02-leo.dev.com)”,  
“msg”: “[papp02-leo.dev.com](http://papp02-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=gapp02-leo.dev.com](http://item=gapp02-leo.dev.com)) =\> {  
“item”: “[gapp02-leo.dev.com](http://gapp02-leo.dev.com)”,  
“msg”: “[gapp02-leo.dev.com](http://gapp02-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=gapp01-leo.dev.com](http://item=gapp01-leo.dev.com)) =\> {  
“item”: “[gapp01-leo.dev.com](http://gapp01-leo.dev.com)”,  
“msg”: “[gapp01-leo.dev.com](http://gapp01-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=relay01-leo.dev.com](http://item=relay01-leo.dev.com)) =\> {  
“item”: “[relay01-leo.dev.com](http://relay01-leo.dev.com)”,  
“msg”: “[relay01-leo.dev.com](http://relay01-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=papp01-leo.dev.com](http://item=papp01-leo.dev.com)) =\> {  
“item”: “[papp01-leo.dev.com](http://papp01-leo.dev.com)”,  
“msg”: “[papp01-leo.dev.com](http://papp01-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=web01-leo.dev.com](http://item=web01-leo.dev.com)) =\> {  
“item”: “[web01-leo.dev.com](http://web01-leo.dev.com)”,  
“msg”: “[web01-leo.dev.com](http://web01-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=relay02-leo.dev.com](http://item=relay02-leo.dev.com)) =\> {  
“item”: “[relay02-leo.dev.com](http://relay02-leo.dev.com)”,  
“msg”: “[relay02-leo.dev.com](http://relay02-leo.dev.com)”  
}  
ok: [localhost] =\> ([item=web02-leo.dev.com](http://item=web02-leo.dev.com)) =\> {  
“item”: “[web02-leo.dev.com](http://web02-leo.dev.com)”,  
“msg”: “[web02-leo.dev.com](http://web02-leo.dev.com)”  
}  
`

How can i get the “role” attributes for each of the hosts? Is there a better way to acheive this rather than using with\_inventory\_hostname. with\_inventory\_hostname allows me to assign the patters like web[01:10] rather than specifying each server instance.

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 11, 2016, 3:35pm UTC](https://forum.ansible.com/t/getting-attributes-from-with-inventory-hostnames/21411/2 "2016-05-11T15:35:44Z")

</div>

Something like:

- debug: msg=“{{ hostvars[item].role }}”  
with\_inventory\_hostnames: “{{env}}”

---

<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 13, 2016, 2:39am UTC](https://forum.ansible.com/t/getting-attributes-from-with-inventory-hostnames/21411/3 "2016-05-13T02:39:23Z")

</div>

Hi Matt,

I got it working.

Thanks,  
Pawan Venugopal.
