# Strange behavior in group\_by module with 'with\_items'

**URL:** <https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312>\
**Category:** Ansible Project\
**Created:** [May 23, 2014, 10:06am UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312 "2014-05-23T10:06:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [May 23, 2014, 10:06am UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312/1 "2014-05-23T10:06:07Z")

</div>

Hi,

I have a couple of hosts that have a ‘profiles’ variable that contains a list of, well, profiles that I want to apply. I want to use group\_by to turn those into groups. However, it is not working. (I am doing this on the current devel branch, by the way.)

This test case isolates the issue:

hosts

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 23, 2014, 12:38pm UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312/2 "2014-05-23T12:38:58Z")

</div>

I’m not positive group\_by can be used with with\_items.

I think we may have proposed making this raise an error, which seems easy to do if this is the case.

If you feel you can boil this down to a minimal reproducer example please file a bug in github and we can take a look. I’d probably just do it with a hard coded list for starters, just to make sure variables have nothing to do with it (as it would provide more information if they did).

Sidenote - it’s very unusual to put variables below your tasks section. I’d suggest not doing that as it makes playbooks less readable.

---

<div class="post-metadata">

**Author:** ![Adam\_Morris](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@Adam\_Morris](https://forum.ansible.com/u/Adam_Morris)\
**Post date:** [May 23, 2014, 2:19pm UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312/3 "2014-05-23T14:19:38Z")

</div>

This seems to be doing what you are telling it to do to me.

Pseudocode:

For every host in the inventory  
&nbsp;&nbsp;For every host-inventoryname  
&nbsp;&nbsp;&nbsp;&nbsp;Add the host to the host-inventoryname group.

The one part I'm not sure about is whether host-inventoryname is being defined once and they are only being put in one group or if they are being put in multiple groups each.

Adam

---

<div class="post-metadata">

**Author:** ![Joost\_Cassee](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@Joost\_Cassee](https://forum.ansible.com/u/Joost_Cassee)\
**Post date:** [May 25, 2014, 8:41pm UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312/4 "2014-05-25T20:41:23Z")

</div>

> I'm not positive group\_by can be used with with\_items.
> 
> I think we may have proposed making this raise an error, which seems easy to  
> do if this is the case.

Well, using a fixed list does work, adding every item as a group. It's  
when a fact (host-specific variable) is used that a problem occurs:  
the value for the first host is used for all hosts.

> If you feel you can boil this down to a minimal reproducer example please  
> file a bug in github and we can take a look. I'd probably just do it with  
> a hard coded list for starters, just to make sure variables have nothing to  
> do with it (as it would provide more information if they did).

I have removed the test\_items variable and submitted an issue:  
[https://github.com/ansible/ansible/issues/7540](https://github.com/ansible/ansible/issues/7540)

> Sidenote - it's very unusual to put variables below your tasks section. I'd  
> suggest not doing that as it makes playbooks less readable.

Yeah, that was probably because I went through stripped down a role to  
a playbook in alphabetical order. 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 25, 2014, 9:16pm UTC](https://forum.ansible.com/t/strange-behavior-in-group-by-module-with-with-items/15312/5 "2014-05-25T21:16:58Z")

</div>

“It’s  
when a fact (host-specific variable) is used that a problem occurs:  
the value for the first host is used for all hosts.”

group\_by was definitely intended to work with fact based variables, in fact, this is the most common pattern:

- group\_by: key=os-{{ ansible\_os\_family }}

We’ll have to look into the looping scope question.

Ticket is filed here for those interested: [https://github.com/ansible/ansible/issues/7540](https://github.com/ansible/ansible/issues/7540)
