Issue 1:
Issue Type: Bug Report

Ansible Version: 1.7.2

Environment: Arch Linux, virtualenv, python 2.7.8

Summary:

Ansible is NOT respecting specified delegate_to in this case !

Steps To Reproduce:

With inventory and playbook in https://gist.github.com/jpic/d7769dbbeb2262f2a29b

Run command

ansible-playbook -v site.yml -i inventory

Expected Results:

TASK: [do something on the kvm hosting the vm] ********************************
changed: [hostb -> kvmb] => {"changed": true, "cmd": "echo domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50", "delta": "0:00:00.002954", "end": "2014-10-23 02:27:50.729417", "rc": 0, "start": "2014-10-23 02:27:50.726463", "stderr": "", "stdout": "domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50"}

TASK: [do something on the kvm hosting the vm] ********************************
changed: [hosta -> kvma] => {"changed": true, "cmd": "echo domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50", "delta": "0:00:00.003011", "end": "2014-10-23 02:27:50.792144", "rc": 0, "start": "2014-10-23 02:27:50.789133", "stderr": "", "stdout": "domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50"}

Actual Results:

TASK: [do something on the kvm hosting the vm] ********************************
changed: [hostb -> kvmb] => {"changed": true, "cmd": "echo domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50", "delta": "0:00:00.002954", "end": "2014-10-23 02:27:50.729417", "rc": 0, "start": "2014-10-23 02:27:50.726463", "stderr": "", "stdout": "domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50"}

TASK: [do something on the kvm hosting the vm] ********************************
changed: [hosta -> kvmb] => {"changed": true, "cmd": "echo domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50", "delta": "0:00:00.003011", "end": "2014-10-23 02:27:50.792144", "rc": 0, "start": "2014-10-23 02:27:50.789133", "stderr": "", "stdout": "domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50"}

Why is hosta task delegated to kvmb in actual results ? -> [hosta -> kvmb]

I wonder, it really looks like a bug to me.

Am I doing anything wrong ?

Is it a regression ? #1241

Here's an interresting debug session:

$ ansible-playbook -i inventory -v site.yml

PLAY [foo] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [hostb]
ok: [hosta]

TASK: [do something on the kvm hosting the vm] ********************************
> /home/jpic/env/lib/python2.7/site-packages/ansible/runner/__init__.py(622)_executor_internal()
621 import ipdb; ipdb.set_trace() ### XXX BREAKPOINT
--> 622 self.module_vars['delegate_to'] = template.template(self.basedir, delegate_to, inject)
623

ipdb> self.module_vars['delegate_to']
'{% if True %}{{ kvm }}{% endif %}'
ipdb> c
changed: [hostb -> kvmb] => {"changed": true, "cmd": "echo domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:45:27", "delta": "0:00:00.004601", "end": "2014-10-23 02:45:33.465961", "rc": 0, "start": "2014-10-23 02:45:33.461360", "stderr": "", "stdout": "domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:45:27"}

TASK: [do something on the kvm hosting the vm] ********************************
> /home/jpic/env/lib/python2.7/site-packages/ansible/runner/__init__.py(622)_executor_internal()
621 import ipdb; ipdb.set_trace() ### XXX BREAKPOINT
--> 622 self.module_vars['delegate_to'] = template.template(self.basedir, delegate_to, inject)
623

ipdb> self.module_vars['delegate_to']
u'kvmb' <-------- it has not been re-interpreted
ipdb>

Issue 2:
Issue Type:

Bug Report

Ansible Version:

all versions

Environment:

N/A

Summary:

There is a bug in the templating code of delegate_to: in that, it only happens once per runner proces fork, instead of for every host.

Steps To Reproduce:

Test inventory and playbook:

$ for n in `seq 0 20`; do echo host$n >> hosts ; done
$ cat test.yml

---
- hosts: all
gather_facts: false
connection: local
tasks:
- command: sleep 1
delegate_to: '{{ inventory_hostname }}'

Expected Results:

ansible-playbook -i hosts test.yml -f 1 (or whatever value for fork)

TASK: [command sleep 1] *******************************************************

changed: [host1 -> host1]

changed: [host0 -> host0]

changed: [host2 -> host2]

changed: [host4 -> host4]

changed: [host8 -> host8]

changed: [host3 -> host3]

changed: [host6 -> host6]

changed: [host9 -> host9]

changed: [host5 -> host5]

changed: [host14 -> host14]

changed: [host16 -> host16]

changed: [host15 -> host15]

changed: [host10 -> host10]

changed: [host13 -> host13]

changed: [host11 -> host11]

changed: [host18 -> host18]

changed: [host17 -> host17]

changed: [host20 -> host20]

changed: [host19 -> host19]

changed: [host7 -> host7]

changed: [host12 -> host12]

Actual Results:

ansible-playbook -i hosts test.yml -f 1

TASK: [command sleep 1] *******************************************************

changed: [host0 -> host0]

changed: [host1 -> host0]

changed: [host2 -> host0]

changed: [host3 -> host0]

changed: [host4 -> host0]

changed: [host5 -> host0]

changed: [host6 -> host0]

changed: [host7 -> host0]

changed: [host8 -> host0]

...

ansible-playbook -i hosts test.yml -f 2

TASK: [command sleep 1] *******************************************************

changed: [host1 -> host1]

changed: [host0 -> host0]

changed: [host3 -> host0]

changed: [host2 -> host1]

changed: [host5 -> host1]

changed: [host4 -> host0]

changed: [host6 -> host1]

changed: [host7 -> host0]

changed: [host9 -> host0]

...

ansible-playbook -i hosts test.yml -f 3

TASK: [command sleep 1] *******************************************************

changed: [host2 -> host2]

changed: [host1 -> host1]

changed: [host0 -> host0]

changed: [host3 -> host2]

changed: [host4 -> host1]

changed: [host5 -> host0]

changed: [host6 -> host2]

changed: [host7 -> host1]

...

The same effect can also be seen by varying the serial: option in the play.