group_by behaving badly?

I’m getting unexpected results from group_by when running two playbooks together. I have two groups with a host each. When I run playbook1 and playbook2 separately, playbook1 is run against host1 and playbook2 is run against host2 as expected. When I include playbook1 and playbook2 in a single playbook, both playbook1 and playbook2 are run against host1. I’d appreciate any suggestions. Running ansible 1.9.4.

hosts
`
[group1]
host1

[group2]
host2
`

Playbook1

`

  • hosts: group1
    tasks:

  • group_by: key={{ ansible_distribution_release }}
    changed_when: False

  • hosts: precise:trusty

gather_facts: False
roles:

  • myrole

`

Playbook2

`

  • hosts: group2
    tasks:

  • group_by: key={{ ansible_distribution_release }}
    changed_when: False

  • hosts: precise:trusty

gather_facts: False
roles:

  • myrole

`

To clarify, the unexpected behavior takes place when running playbook3:

Playbook3

`

  • include: playbook1.yml
  • include: playbook2.yml

`

I would actually expect that the 2nd play in playbook2 runs for any
hosts that are in the precise or trusty group, this would be normal
behaviour as you called group_by twice and in each case inserted a
host into one of those groups. group_by does not delete existing hosts
from groups.

In that case, when running playbook3, shouldn’t playbook2 be run for both host1 and host2? It’s only run for host1.
Cheers.

SA

I decided to rely on this workaround that makes groups unique:

`

  • hosts: group1
    tasks:

  • group_by: key=-group1-{{ ansible_distribution_release }}
    changed_when: False

  • hosts: group1-precise:group1-trusty
    gather_facts: False
    roles:

  • myrole

`

`

  • hosts: group2
    tasks:

  • group_by: key=-group2-{{ ansible_distribution_release }}
    changed_when: False

  • hosts: group2-precise:group2-trusty
    gather_facts: False
    roles:

  • myrole

`