# bug in "with\_items" or misunderstanding?

**URL:** <https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829>\
**Category:** Ansible Project\
**Created:** [November 19, 2014, 8:22pm UTC](https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829 "2014-11-19T20:22:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [November 19, 2014, 8:22pm UTC](https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829/1 "2014-11-19T20:22:36Z")

</div>

I have an inventory of hosts that defines “my\_groups” hash:

my\_groups:  
group1=val1  
group2=val2  
…

Note - it’s a dynamic inventory which in fact produces valid JSON that works otherwise.

Then I try to use it to auto-populate groups via:

- hosts: all  
tasks:
- debug: var=hostvars[inventory\_hostname][‘my\_groups’].keys()
- set\_fact: my\_group\_list=“{{ hostvars[inventory\_hostname][‘my\_groups’].keys() }}”  
when: hostvars[inventory\_hostname].my\_groups is defined
- debug: var=my\_group\_list
- group\_by: key={{ item }}\_group  
with\_items: hostvars[inventory\_hostname].my\_group\_list  
when: my\_group\_list is defined

and receive lovely:

…

TASK: [group\_by key={{ item }}\_group] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [[moo.foo.com](http://moo.foo.com)] =\> with\_items expects a list or a set

…

even though last debug confirms that my\_group\_list is a proper list. When I used “my\_group\_list” instead - it acted as if “my\_group\_list” was the item to iterate over rather than variable:

TASK: [group\_by key={{ item }}\_group] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<[moo.foo.com](http://moo.foo.com)\> ESTABLISH CONNECTION FOR USER: root  
created ‘group\_by’ ActionModule: key=my\_group\_list\_group  
changed: [[moo.foo.com](http://moo.foo.com)] =\> (item=my\_group\_list) =\> {“changed”: true, “groups”: {“my\_group\_list\_group”: [“[moo.foo.com](http://moo.foo.com)”, “[bar.foo.com](http://bar.foo.com)”]}, “item”: “my\_group\_list”}

Am I doing something wrong or am I facing a bug?

$ rpm -q redhat-release-server  
redhat-release-server-6Server-6.6.0.2.el6.x86\_64

$ rpm -q ansible

ansible-1.7.2-2.el6.noarch

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 19, 2014, 9:07pm UTC](https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829/2 "2014-11-19T21:07:19Z")

</div>

try this:

- group\_by: key={{ item }}\_group  
with\_items: my\_group\_list|list

btw, this won’t do what you think it will, it gets evaluated for each iteration of with\_items  
when: my\_group\_list is defined

---

<div class="post-metadata">

**Author:** ![Dimon](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dimon](https://forum.ansible.com/u/Dimon)\
**Post date:** [November 20, 2014, 4:01pm UTC](https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829/3 "2014-11-20T16:01:22Z")

</div>

So I have modified my playbook (see [https://gist.github.com/droopy4096/98864a10359f5cf27bab](https://gist.github.com/droopy4096/98864a10359f5cf27bab) ), then played with “|list” but the results of it were that all of sudden it grabs JSON string and walks through that string one char at a time, resulting in:

changed: [[undefined.host.com](http://undefined.host.com)] =\> (item=[) =\> {“changed”: true, “groups”: {“[\_group”: [“[undefined.host.com](http://undefined.host.com)”, “[defined.host.com](http://defined.host.com)”]}, “item”: “[”}

fatal: [[undefined.host.com](http://undefined.host.com)] =\> error parsing argument string ‘key="\_group’, try quoting the entire line.

If you look carefully at the gist - when I do the same “loop” “with\_items” using “debug:” - everything works as expected, with “group\_by” it gets screwed up in a major fashion.

I can invert the question and ask: given the existing dynamic inventory script, which can add some more “facts” per-host - I’d like to [re]create groups based on some of those “facts”. What is the best approach?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [November 20, 2014, 6:25pm UTC](https://forum.ansible.com/t/bug-in-with-items-or-misunderstanding/16829/4 "2014-11-20T18:25:29Z")

</div>

there is also a from\_json filter, sometimes it is hard to see what is  
json in a var as the output is normally 'jsonized'.
