Using an inventory group with_Items in playbook and in template

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.

You want:

{{ hostvars[item].inventory_hostname }}

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

That works. My expectation was that groups.some_group would return a collection of inventory items complete with attributes. Anyway, thanks!