Issue 1:
Issue Type:

Bug Report

Ansible Version:

ansible 1.7 (devel ee82681) last updated 2014/05/23 10:56:18 (GMT +200)

Environment:

Ubuntu 14.04

Summary:

Using group_by with with_items, host-specific variables seem to be resolved only for the first host.

Steps To Reproduce:

hosts:

[group]
host1
host2

playbook:

- hosts: all
connection: local
gather_facts: no
tasks:
- debug: var=inventory_hostname
- group_by: key={{ item }}
with_items:
- "{{ inventory_hostname }}"
- debug: var=group_names

Run ansible-playbook:

$ ansible-playbook -i hosts test.yml

Expected Results:

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

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

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

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

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

Actual Results:

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

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

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

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

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

Differences:

Note two things:

host2 is in group host1 instead of host2.

(cosmetic) There is only one status line for the group_by task.

Further info:

Using with_items without host-specific variables does work, but still only shows status lines for the first host:

- hosts: all
connection: local
gather_facts: no
tasks:
- debug: var=inventory_hostname
- group_by: key={{ item }}
with_items:
- test1
- test2
- test3
- debug: var=group_names

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

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

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

TASK: [debug var=group_names] *************************************************
ok: [host1] => {
"group_names": [
"group",
"test1",
"test2",
"test3"
]
}
ok: [host2] => {
"group_names": [
"group",
"test1",
"test2",
"test3"
]
}

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

Using a host-specific variable is in itself also not a problem:

- hosts: all
connection: local
gather_facts: no
tasks:
- debug: var=inventory_hostname
- group_by: key={{ inventory_hostname }}
- debug: var=group_names

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

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

TASK: [group_by key={{inventory_hostname}}] ***********************************
changed: [host1]

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

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

Issue 2:
Issue Type:

Bug Report

Ansible Version:

ansible 1.5.4

Environment:

Ubuntu 12.04 x64 running Ansible, managing 13.10 x64 hosts

Summary:

Attempting to use 'group_by:' with 'when:'.

Two hosts; one in group dbnodes, one in group webnodes, both also a member of ec2_hosts.

One host satisfies the when clause, one does not.

Flipping group membership doesn't flip which host is grouped...just skips the task altogether.

Steps To Reproduce:

See gist: https://gist.github.com/mark-casey/11386608

One provided inventory works for me, one doesn't.

Expected Results:

The host which is a member of 'dbnodes' should be added to the 'Debian' group, for either of the two inventory files provided.

Actual Results:

One of the inventory files sees no hosts added to the 'Debian' group.

Also interesting, either of the two inventories will work if you entirely remove the ec2_hosts group and change site.yml's play to use this host selector instead:

hosts: dbnodes:webnodes