# with\_items: groups\[\]

**URL:** <https://forum.ansible.com/t/with-items-groups/13503>\
**Category:** Ansible Project\
**Created:** [September 18, 2013, 7:49pm UTC](https://forum.ansible.com/t/with-items-groups/13503 "2013-09-18T19:49:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Samuel\_Martin\_Moro](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Samuel\_Martin\_Moro](https://forum.ansible.com/u/Samuel_Martin_Moro)\
**Post date:** [September 18, 2013, 7:49pm UTC](https://forum.ansible.com/t/with-items-groups/13503/1 "2013-09-18T19:49:09Z")

</div>

Hi,

Assuming ansible-1.3 from pip, python 2.7.3 from wheezy.

Using something like:

- include: something.yml  
with\_items: groups[‘all’]

I have some error about with\_items requiring a list.

Meanwhile, the further works:

- name: toto  
file: src=client.j2 dest=/etc/munin/objects/{{ item }}.conf […]  
with\_items: groups[‘all’]

Not sure if we could call that a bug. Whatever.  
Now for what’s really bugging me tonight: I’m trying to configure my munin workers, so all hosts addressed during current ansible run gets defined/updated.

Using this:

- name: ensure hosts from current run gets munin-monitored  
template: src=/etc/ansible/roles/munin/templates/client.j2 dest=/etc/munin/objects/{{ item }}.conf owner=root group={{ gid\_zero }} mode=0644  
with\_items: groups[‘all’]

when: hostvars[{{ item }}][‘ansible\_default\_ipv4’][‘address’] is defined

It seems that my when clause never evaluates to true, even iterating on local host.

Commenting out the when, template fails resolving ‘ansible\_default\_ipv4’:

fatal: [[munin.example.com](http://munin.example.com)] =\> {‘msg’: “One or more undefined variables: ‘dict object’ has no attribute ‘ansible\_default\_ipv4’”, ‘failed’: True}  
fatal: [[munin.example.com](http://munin.example.com)] =\> {‘msg’: ‘One or more items failed.’, ‘failed’: True, ‘changed’: False, ‘results’: [{‘msg’: “One or more undefined variables: ‘dict object’ has no attribute ‘ansible\_default\_ipv4’”, ‘failed’: True}]}

with some template like:  
[{{ item }}]

address {{ hostvars[item][‘ansible\_default\_ipv4’][‘address’] }}  
use\_node\_name yes

I was looking at [www.ansibleworks.com/docs/playbook2.html#id7](http://www.ansibleworks.com/docs/playbook2.html#id7)  
The last sample (host in groups[‘app\_servers’]) looks pretty much like my failing template. Except I’m looping from the playbook.

Have I missed something?

Thanks,

Regards.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [September 19, 2013, 10:12am UTC](https://forum.ansible.com/t/with-items-groups/13503/2 "2013-09-19T10:12:32Z")

</div>

Could you open a github ticket for the first issue? It does seem like it’s inconsistent behavior for sure.

For the second issue, did you run the gather\_facts step before this? If you’re trying to iterate over other hosts facts but have not yet connected to them, the data will not be there. We have an open feature request to create a lookaside cache to help overcome this situation.

Just to be sure it’s not some other issue, you can verify that the ansible\_default\_ipv4 structure is being filled by running “ansible -m setup ” and look through the JSON it returns. It is possible that it’s not being picked up correctly by the setup module but most likely it’s what I have described above.

Thanks!

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [September 19, 2013, 12:16pm UTC](https://forum.ansible.com/t/with-items-groups/13503/3 "2013-09-19T12:16:05Z")

</div>

Hi,

I had the same kind of looping through hostvars question …

> - name: ensure hosts from current run gets munin-monitored  
> template: src=/etc/ansible/roles/munin/templates/client.j2 dest=/etc/munin/objects/{{ item }}.conf owner=root group={{ gid\_zero }} mode=0644  
> with\_items: groups[‘all’]
> 
> when: hostvars[{{ item }}][‘ansible\_default\_ipv4’][‘address’] is defined

you should try using {{ hostvars[item][‘ansible\_default\_ipv4’][‘address’] }}

hope this help.

Fred

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 19, 2013, 12:30pm UTC](https://forum.ansible.com/t/with-items-groups/13503/4 "2013-09-19T12:30:46Z")

</div>

include cannot be used with host or group vars, not sure if this applies to groups themselves also.

---

<div class="post-metadata">

**Author:** ![Samuel\_Martin\_Moro](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Samuel\_Martin\_Moro](https://forum.ansible.com/u/Samuel_Martin_Moro)\
**Post date:** [September 19, 2013, 1:21pm UTC](https://forum.ansible.com/t/with-items-groups/13503/5 "2013-09-19T13:21:26Z")

</div>

> Could you open a github ticket for the first issue? It does seem like it's inconsistent behavior for sure.

I did.  
This is expected.  
[https://github.com/ansible/ansible/issues/4171](https://github.com/ansible/ansible/issues/4171)  
Sure, why not.

> For the second issue, did you run the gather\_facts step before this? If you're trying to iterate over other hosts facts but have not yet connected to them, the data will not be there. We have an open feature request to create a lookaside cache to help overcome this situation.

I haven't disabled gather\_facts. These variables are available from local scope.

Investigating, I modified my template to look like this:

[{{ item }}]  
{% if hostvars[item]['ansible\_default\_ipv4'] is defined %}  
{% if hostvars[item]['ansible\_default\_ipv4']['address'] is defined %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;address {{ hostvars[item]['ansible\_default\_ipv4']['address'] }}  
{% else %}  
{% for stuff in hostvars[item]['ansible\_default\_ipv4'] %}  
{{ stuff }}  
{% endfor %}  
{% endif %}  
{% else %}  
{% for stuff in hostvars[item] %}  
{{ stuff }}  
{% endfor %}  
{% endif %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use\_node\_name yes

Having the task:  
- name: ensure hosts from current run gets munin-monitored  
&nbsp;&nbsp;&nbsp;template: src=/x/munin/templates/client.j2 dest=/etc/munin/objects/{{ item }}.  
&nbsp;&nbsp;&nbsp;with\_items: groups['all']  
&nbsp;&nbsp;&nbsp;when: hostvars[{{ item }}] is defined #always evaluates to true

The templates are correctly evaluated:  
muninone:~# cat /etc/munin/objects/muninone.example.com.conf  
[muninone.example.com]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;address 10.42.242.205  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use\_node\_name yes  
muninone:~# cat /etc/munin/objects/munintoo.example.com.conf  
[munintoo.example.com]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;address 10.42.242.220  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use\_node\_name yes

So I can indeed reach hostvars[{{ item }}]['ansible\_default\_ipv4'] from my template.

Now if I change my task to:  
- name: ensure hosts from current run gets munin-monitored  
&nbsp;&nbsp;&nbsp;template: src=/x/munin/templates/client.j2 dest=/etc/munin/objects/{{ item }}.  
&nbsp;&nbsp;&nbsp;with\_items: groups['all']  
&nbsp;&nbsp;&nbsp;when: hostvars[{{ item }}]['ansible\_default\_ipv4'] is defined

muninone and munintoo iteration get skipped.

ansible:~/playbooks# ansible-playbook -i production centos.yml -l muninservices

PLAY [centos]

---

<div class="post-metadata">

**Author:** ![Samuel\_Martin\_Moro](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Samuel\_Martin\_Moro](https://forum.ansible.com/u/Samuel_Martin_Moro)\
**Post date:** [September 19, 2013, 1:30pm UTC](https://forum.ansible.com/t/with-items-groups/13503/6 "2013-09-19T13:30:50Z")

</div>

Well, seems not.  
Maybe was it possible with 1.2.2? I noticed some syntax changes, either in templates en when: clauses, since 1.3.

ansible:~/playbooks# ansible-playbook -i production centos.yml -l muninservices  
ERROR: Syntax Error while loading YAML script, /x/munin/tasks/server/suckit.yml  
Note: The error may actually appear before this position: line 5, column 20

&nbsp;&nbsp;&nbsp;with\_items: groups['all']  
&nbsp;&nbsp;&nbsp;when: {{ hostvars[item]['ansible\_default\_ipv4'] }} is defined

Using either:  
&nbsp;&nbsp;&nbsp;when: {{ hostvars[item]['ansible\_default\_ipv4'] }}  
Or:  
&nbsp;&nbsp;&nbsp;when: {{ hostvars[item]['ansible\_default\_ipv4'] }} is defined

---

<div class="post-metadata">

**Author:** ![Fred\_Badel](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@Fred\_Badel](https://forum.ansible.com/u/Fred_Badel)\
**Post date:** [September 19, 2013, 1:43pm UTC](https://forum.ansible.com/t/with-items-groups/13503/7 "2013-09-19T13:43:17Z")

</div>

You should quote it, try:

with\_item: groups[‘all’]  
when: “{{ hostvars[item][‘ansible\_default\_ipv4’] }}”

Fred

---

<div class="post-metadata">

**Author:** ![Samuel\_Martin\_Moro](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Samuel\_Martin\_Moro](https://forum.ansible.com/u/Samuel_Martin_Moro)\
**Post date:** [September 19, 2013, 2:19pm UTC](https://forum.ansible.com/t/with-items-groups/13503/8 "2013-09-19T14:19:40Z")

</div>

Still failing:

- name: ensure hosts from current run gets munin-monitored  
&nbsp;&nbsp;&nbsp;template: src=/x/munin/templates/client.j2 dest=/etc/munin/objects/{{ item }}.conf ...  
&nbsp;&nbsp;&nbsp;with\_items: groups['all']  
&nbsp;&nbsp;&nbsp;when: "{{ hostvars[item]['ansible\_default\_ipv4'] }}"

TASK: [ensure hosts from current run gets munin-monitored]

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 20, 2013, 1:24am UTC](https://forum.ansible.com/t/with-items-groups/13503/9 "2013-09-20T01:24:33Z")

</div>

You’re making template usage a bit too complicated, really, and this results in your problem.

when: “{{ hostvars[item][‘ansible\_default\_ipv4’] }}” is defined

Can be written as

when: hostvars[item][‘ansible\_default\_ipv4’] is defined
