Using an inventory group with_Items in playbook and in template

3,105 views
Skip to first unread message

Rick O'Shea

unread,
Apr 17, 2016, 10:19:13 PM4/17/16
to Ansible Project
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.  

Matt Martz

unread,
Apr 17, 2016, 10:27:46 PM4/17/16
to ansible...@googlegroups.com
You want:

{{ hostvars[item].inventory_hostname }}
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/48938ce6-29eb-4333-95f3-fce5c11cf26f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Jason Gilfoil

unread,
Apr 17, 2016, 10:40:39 PM4/17/16
to Ansible Project
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


On Sunday, April 17, 2016 at 10:27:46 PM UTC-4, Matt Martz wrote:
You want:

{{ hostvars[item].inventory_hostname }}

On Sunday, April 17, 2016, Rick O'Shea <cogi...@gmail.com> wrote:
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 received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

Rick O'Shea

unread,
Apr 17, 2016, 11:47:54 PM4/17/16
to Ansible Project
That works. My expectation was that groups.some_group would return a collection of inventory items complete with attributes.  Anyway, thanks! 
Reply all
Reply to author
Forward
0 new messages