<h5 dir="auto">Issue Type: Bug Report</h5>
<h5 dir="auto">Ansible Version: 1.7.2</h5>
<h5 dir="auto">Environment: Arch Linux, virtualenv, python 2.7.8</h5>
<h5 dir="auto">Summary:</h5>
<p dir="auto">Ansible is <strong>NOT</strong> respecting specified delegate_to in this case !</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">With inventory and playbook in <a href="https://gist.github.com/jpic/d7769dbbeb2262f2a29b">https://gist.github.com/jpic/d7769dbbeb2262f2a29b</a></p>
<p dir="auto">Run command</p>
<p dir="auto">ansible-playbook  -v site.yml -i inventory</p>
<h5 dir="auto">Expected Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hostb -&gt; kvmb] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50&quot;, &quot;delta&quot;: &quot;0:00:00.002954&quot;, &quot;end&quot;: &quot;2014-10-23 02:27:50.729417&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-10-23 02:27:50.726463&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50&quot;}

TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hosta -&gt; kvma] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50&quot;, &quot;delta&quot;: &quot;0:00:00.003011&quot;, &quot;end&quot;: &quot;2014-10-23 02:27:50.792144&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-10-23 02:27:50.789133&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50&quot;}"><pre class="notranslate"><code class="notranslate">TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hostb -&gt; kvmb] =&gt; {"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 -&gt; kvma] =&gt; {"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"}
</code></pre></div>
<h5 dir="auto">Actual Results:</h5>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hostb -&gt; kvmb] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50&quot;, &quot;delta&quot;: &quot;0:00:00.002954&quot;, &quot;end&quot;: &quot;2014-10-23 02:27:50.729417&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-10-23 02:27:50.726463&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;domain=hostb kvm=kvmb snapshot_name=MEP-2014-10-23-02:27:50&quot;}

TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hosta -&gt; kvmb] =&gt; {&quot;changed&quot;: true, &quot;cmd&quot;: &quot;echo domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50&quot;, &quot;delta&quot;: &quot;0:00:00.003011&quot;, &quot;end&quot;: &quot;2014-10-23 02:27:50.792144&quot;, &quot;rc&quot;: 0, &quot;start&quot;: &quot;2014-10-23 02:27:50.789133&quot;, &quot;stderr&quot;: &quot;&quot;, &quot;stdout&quot;: &quot;domain=hosta kvm=kvma snapshot_name=MEP-2014-10-23-02:27:50&quot;}"><pre class="notranslate"><code class="notranslate">TASK: [do something on the kvm hosting the vm] ******************************** 
changed: [hostb -&gt; kvmb] =&gt; {"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 -&gt; kvmb] =&gt; {"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"}
</code></pre></div>
<p dir="auto"><strong>Why is hosta task delegated to kvmb in actual results ?</strong> -&gt; <code class="notranslate">[hosta -&gt; kvmb]</code></p>
<p dir="auto">I wonder, it really looks like a bug to me.</p>
<p dir="auto">Am I doing anything wrong ?</p>
<p dir="auto">Is it a regression ? <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="7379957" data-permission-text="Title is private" data-url="https://github.com/ansible/ansible/issues/1241" data-hovercard-type="issue" data-hovercard-url="/ansible/ansible/issues/1241/hovercard" href="https://github.com/ansible/ansible/issues/1241">#1241</a></p>
<p dir="auto">Here's an interresting debug session:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ ansible-playbook -i inventory -v site.yml 

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

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

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

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

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

ipdb&gt; self.module_vars['delegate_to']
u'kvmb' &lt;-------- it has not been re-interpreted
ipdb&gt; "><pre class="notranslate"><code class="notranslate">$ ansible-playbook -i inventory -v site.yml 

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

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

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

ipdb&gt; self.module_vars['delegate_to']
'{% if True %}{{ kvm }}{% endif %}'
ipdb&gt; c
changed: [hostb -&gt; kvmb] =&gt; {"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] ******************************** 
&gt; /home/jpic/env/lib/python2.7/site-packages/ansible/runner/__init__.py(622)_executor_internal()
    621             import ipdb; ipdb.set_trace() ### XXX BREAKPOINT
--&gt; 622             self.module_vars['delegate_to'] = template.template(self.basedir, delegate_to, inject)
    623 

ipdb&gt; self.module_vars['delegate_to']
u'kvmb' &lt;-------- it has not been re-interpreted
ipdb&gt; 
</code></pre></div>
<h5 dir="auto">Issue Type:</h5>
<p dir="auto">Bug Report</p>
<h5 dir="auto">Ansible Version:</h5>
<p dir="auto">all versions</p>
<h5 dir="auto">Environment:</h5>
<p dir="auto">N/A</p>
<h5 dir="auto">Summary:</h5>
<p dir="auto">There is a bug in the templating code of <code class="notranslate">delegate_to:</code> in that, it only happens once per runner proces fork, instead of for every host.</p>
<h5 dir="auto">Steps To Reproduce:</h5>
<p dir="auto">Test inventory and playbook:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ for n in `seq 0 20`; do echo host$n &gt;&gt; hosts ; done
$ cat test.yml

---
- hosts: all
  gather_facts: false
  connection: local
  tasks:
    - command: sleep 1
      delegate_to: '{{ inventory_hostname }}'"><pre class="notranslate"><code class="notranslate">$ for n in `seq 0 20`; do echo host$n &gt;&gt; hosts ; done
$ cat test.yml

---
- hosts: all
  gather_facts: false
  connection: local
  tasks:
    - command: sleep 1
      delegate_to: '{{ inventory_hostname }}'
</code></pre></div>
<h5 dir="auto">Expected Results:</h5>
<p dir="auto"><code class="notranslate">ansible-playbook -i hosts test.yml -f 1</code> (or whatever value for fork)</p>
<p dir="auto">TASK: [command sleep 1] *******************************************************<br>
changed: [host1 -&gt; host1]<br>
changed: [host0 -&gt; host0]<br>
changed: [host2 -&gt; host2]<br>
changed: [host4 -&gt; host4]<br>
changed: [host8 -&gt; host8]<br>
changed: [host3 -&gt; host3]<br>
changed: [host6 -&gt; host6]<br>
changed: [host9 -&gt; host9]<br>
changed: [host5 -&gt; host5]<br>
changed: [host14 -&gt; host14]<br>
changed: [host16 -&gt; host16]<br>
changed: [host15 -&gt; host15]<br>
changed: [host10 -&gt; host10]<br>
changed: [host13 -&gt; host13]<br>
changed: [host11 -&gt; host11]<br>
changed: [host18 -&gt; host18]<br>
changed: [host17 -&gt; host17]<br>
changed: [host20 -&gt; host20]<br>
changed: [host19 -&gt; host19]<br>
changed: [host7 -&gt; host7]<br>
changed: [host12 -&gt; host12]</p>
<h5 dir="auto">Actual Results:</h5>
<p dir="auto"><code class="notranslate">ansible-playbook -i hosts test.yml -f 1</code><br>
TASK: [command sleep 1] *******************************************************<br>
changed: [host0 -&gt; host0]<br>
changed: [host1 -&gt; host0]<br>
changed: [host2 -&gt; host0]<br>
changed: [host3 -&gt; host0]<br>
changed: [host4 -&gt; host0]<br>
changed: [host5 -&gt; host0]<br>
changed: [host6 -&gt; host0]<br>
changed: [host7 -&gt; host0]<br>
changed: [host8 -&gt; host0]<br>
...</p>
<p dir="auto"><code class="notranslate">ansible-playbook -i hosts test.yml -f 2</code><br>
TASK: [command sleep 1] *******************************************************<br>
changed: [host1 -&gt; host1]<br>
changed: [host0 -&gt; host0]<br>
changed: [host3 -&gt; host0]<br>
changed: [host2 -&gt; host1]<br>
changed: [host5 -&gt; host1]<br>
changed: [host4 -&gt; host0]<br>
changed: [host6 -&gt; host1]<br>
changed: [host7 -&gt; host0]<br>
changed: [host9 -&gt; host0]<br>
...</p>
<p dir="auto"><code class="notranslate">ansible-playbook -i hosts test.yml -f 3</code><br>
TASK: [command sleep 1] *******************************************************<br>
changed: [host2 -&gt; host2]<br>
changed: [host1 -&gt; host1]<br>
changed: [host0 -&gt; host0]<br>
changed: [host3 -&gt; host2]<br>
changed: [host4 -&gt; host1]<br>
changed: [host5 -&gt; host0]<br>
changed: [host6 -&gt; host2]<br>
changed: [host7 -&gt; host1]<br>
...</p>
<p dir="auto">The same effect can also be seen by varying the <code class="notranslate">serial:</code> option in the play.</p>