Random inclusion of irrelevant hosts

I have a question related to random inclusion of irrelevant hosts. Below is a part of my playbook:

- hosts: machine01 machine02 machine03 machine04
gather_facts: no
connection: docker
tasks:
- include: common/setup3.yml

- hosts: machine05 machine06
gather_facts: no
connection: docker
tasks:
- include: common/setup2.yml

setup3.yml includes following lines:

- name: restart sshd
command: service ssh restart

setup2.yml:

- name: restart sshd
service: name=sshd state=restarted

However, what sometimes happens is when I run the playbook, eventually I get the following output and crash:

TASK [INCLUDE] ******************************************************
Friday 22 July 2016 15:00:01
included: /opt/info/ansible/common/setup2.yml for machine05 machine06, machine03

TASK [restart sshd] ****************************************************
Friday 22 July 2016 15:00:02
changed: [machine 05]
fatal: [machine03]: FAILED! => {“changed”: false, “failed”: true, “msg”: “no service or tool found for: sshd”}
changed: [machine 06]

So what happens, is for whatever reason setup2.yml includes machine03 (or some other random machine) to it’s hosts and crashes, because the play is not applicable for the machine in question.

What might be the reason for this random inclusion of irrelevant hosts? This seems to happen completely randomly in about 20% of cases.

Thank you in advance

So I tried to reproduce this and examined the code step by step, I don’t see how this can happen, much less randomly.

The only things i can think of is that you have overlapping host/group names and are using group_by to add the machine to a ‘group with host name’, but this should happen every run, not at random.

If you can create a reproducer please open a ticket at https://github.com/ansible/ansible