# Using an inventory group with\_Items in playbook and in template

**URL:** <https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185>\
**Category:** Ansible Project\
**Created:** [April 18, 2016, 2:19am UTC](https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185 "2016-04-18T02:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_O\_Shea](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Rick\_O\_Shea](https://forum.ansible.com/u/Rick_O_Shea)\
**Post date:** [April 18, 2016, 2:19am UTC](https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185/1 "2016-04-18T02:19:13Z")

</div>

This sort of works:

_- hosts: master_  
_tasks:_  
_- name loop through slaves and copy template results to master_  
_template: src=template.txt dest={item}.txt_  
_with\_items: {{groups.slaves}}_

What I really want is this but it fails since **item** “has no attribute ‘inventor\_hostname’”:

_- hosts: master_  
_tasks:_  
_- name loop through slaves and copy template results to master_  
_template: src=template.txt dest={item. **inventory\_hostname** }.txt_  
_with\_items_: {{groups.slaves}}

Assuming that can be made to work, the template.txt also assumes it has access to a host, complete with host variables etc.

---

<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:** [April 18, 2016, 2:27am UTC](https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185/2 "2016-04-18T02:27:39Z")

</div>

You want:

{{ hostvars[item].inventory\_hostname }}

---

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [April 18, 2016, 2:40am UTC](https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185/3 "2016-04-18T02:40:39Z")

</div>

Fantastic timing on this question! I was just about to post the same thing. Also, since it wasn’t initially clear for me, i’ll post for the benefit others:

You can use it this way:

_- debug: msg=“{{ hostvars[item].ansible\_host }}”_  
_with\_items: groups.f5loadbalancers_

---

<div class="post-metadata">

**Author:** ![Rick\_O\_Shea](https://avatars.discourse-cdn.com/v4/letter/r/65b543/32.png) [@Rick\_O\_Shea](https://forum.ansible.com/u/Rick_O_Shea)\
**Post date:** [April 18, 2016, 3:47am UTC](https://forum.ansible.com/t/using-an-inventory-group-with-items-in-playbook-and-in-template/21185/4 "2016-04-18T03:47:54Z")

</div>

That works. My expectation was that _groups.some\_group_ would return a collection of inventory items complete with attributes. Anyway, thanks!
