Strange behavior in group_by module with 'with_items'

77 views
Skip to first unread message

Joost Cassee

unread,
May 23, 2014, 6:06:07 AM5/23/14
to ansible...@googlegroups.com
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
-----
host1
host2

test.yml
--------
- hosts: all
  connection: local
  gather_facts: no
  tasks:
  - debug: var=test_items
  - group_by: key=host_{{ item }}
    with_items: test_items
  - debug: var=group_names
  vars:
    test_items:
    - "{{ inventory_hostname }}"

$ ansible-playbook test.yml -i hosts

PLAY [all] ********************************************************************

TASK: [debug var=test_items] **************************************************
ok: [host2] => {
    "test_items": [
        "host2"
    ]
}
ok: [host1] => {
    "test_items": [
        "host1"
    ]
}

TASK: [group_by key=host_{{item}}] ********************************************
changed: [host1] => (item=host1)

TASK: [debug var=group_names] *************************************************
ok: [host1] => {
    "group_names": [
        "host_host1",
        "ungrouped"
    ]
}
ok: [host2] => {
    "group_names": [
        "host_host1",
        "ungrouped"
    ]
}

PLAY RECAP ********************************************************************
host1                      : ok=3    changed=1    unreachable=0    failed=0  
host2                      : ok=3    changed=1    unreachable=0    failed=0  

Notice how both hosts are in group 'host_host1', even host2.

Is this expected behavior (i.e. does group_by not work with with_items) or should I create an issue on GitHub?

Regards,
Joost

Michael DeHaan

unread,
May 23, 2014, 8:39:01 AM5/23/14
to ansible...@googlegroups.com
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.




--
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/b2ff3403-a35a-4858-ba27-1836f28ea136%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adam Morris

unread,
May 23, 2014, 10:19:38 AM5/23/14
to ansible...@googlegroups.com
This seems to be doing what you are telling it to do to me.

Pseudocode:

For every host in the inventory
For every host-inventoryname
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

Joost Cassee

unread,
May 25, 2014, 4:42:10 PM5/25/14
to ansible...@googlegroups.com
2014-05-23 14:38 GMT+02:00 Michael DeHaan <mic...@ansible.com>:
> 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

> 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. :-)

2014-05-23 16:19 GMT+02:00 Adam Morris <zwac...@gmail.com>:
> This seems to be doing what you are telling it to do to me.
>
> [...]
>
> 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.

That is my point. I would expect every host to be put in their own
group. If you us a fact but not with_items it works. I have included
an example in the issue.

Regards,
Joost

--
Joost Cassee
http://joost.cassee.net

Michael DeHaan

unread,
May 25, 2014, 5:17:01 PM5/25/14
to ansible...@googlegroups.com
"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




--
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.
Reply all
Reply to author
Forward
0 new messages