Repository: ansible/ansible

Issue:
delegate_to not resolved in module
running the following task:

```
- name: "put the configuration on all dispatcher hosts"
  template: 
      src=template_name.nginx.conf
      dest=/etc/nginx/sites-available/vhostname.domainname
  delegate_to: "{{ item }}"
  with_items: "{{ dispatchers }}"
```

where dispatchers is a list of hosts e.g. ['dispatcher.local']
fails for me with:

```
fatal: [ninja.local -> dispatcher.lcoal] => Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 594, in _executor
  exec_rc = self._executor_internal(host, new_stdin)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 822, in _executor_internal
  complex_args=complex_args
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 1032, in _executor_internal_inner
  result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/action_plugins/template.py", line 94, in run
  remote_checksum = self.runner._remote_checksum(conn, tmp, dest, inject)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 1238, in _remote_checksum
  python_interp = inject['hostvars'][inject['delegate_to']].get('ansible_python_interpreter', 'python')
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 103, in __getitem__
  result = self.inventory.get_variables(host, vault_password=self.vault_password).copy()
File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 442, in get_variables
  raise Exception("host not found: %s" % hostname)
Exception: host not found: {{ item }}
```

It appears 'delegate_to' is resolved to correct host in https://github.com/ansible/ansible/blob/release1.8.2/lib/ansible/runner/__init__.py#L921 but not reinserted into 'inject' 

the following patch to /lib/ansible/runner/**init**.py seems to work (at least for me)

```
932            inject = delegate['inject']
+++            # set resolved delegate_to into inject so modules can call _remote_checksum
+++            inject['delegate_to'] = self.delegate_to
```

delegate_to not resolved in module
running the following task:

```
- name: "put the configuration on all dispatcher hosts"
  template: 
      src=template_name.nginx.conf
      dest=/etc/nginx/sites-available/vhostname.domainname
  delegate_to: "{{ item }}"
  with_items: "{{ dispatchers }}"
```

where dispatchers is a list of hosts e.g. ['dispatcher.local']
fails for me with:

```
fatal: [ninja.local -> dispatcher.lcoal] => Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 594, in _executor
  exec_rc = self._executor_internal(host, new_stdin)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 822, in _executor_internal
  complex_args=complex_args
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 1032, in _executor_internal_inner
  result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/action_plugins/template.py", line 94, in run
  remote_checksum = self.runner._remote_checksum(conn, tmp, dest, inject)
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 1238, in _remote_checksum
  python_interp = inject['hostvars'][inject['delegate_to']].get('ansible_python_interpreter', 'python')
File "/usr/local/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 103, in __getitem__
  result = self.inventory.get_variables(host, vault_password=self.vault_password).copy()
File "/usr/local/lib/python2.7/dist-packages/ansible/inventory/__init__.py", line 442, in get_variables
  raise Exception("host not found: %s" % hostname)
Exception: host not found: {{ item }}
```

It appears 'delegate_to' is resolved to correct host in https://github.com/ansible/ansible/blob/release1.8.2/lib/ansible/runner/__init__.py#L921 but not reinserted into 'inject' 

the following patch to /lib/ansible/runner/**init**.py seems to work (at least for me)

```
932            inject = delegate['inject']
+++            # set resolved delegate_to into inject so modules can call _remote_checksum
+++            inject['delegate_to'] = self.delegate_to
```



Discussion:
Thank you... 

I just stumbled upon this bug.

I can also confirm that the patch fixes it.
Please merge.

Thank you... 

I just stumbled upon this bug.

I can also confirm that the patch fixes it.
Please merge.


Resolving patch:
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -934,6 +934,8 @@ def _executor_internal_inner(self, host, module_name, module_args, inject, port,
             actual_private_key_file = delegate['private_key_file']
             self.sudo_pass = delegate['sudo_pass']
             inject = delegate['inject']
+            # set resolved delegate_to into inject so modules can call _remote_checksum
+            inject['delegate_to'] = self.delegate_to
 
         # user/pass may still contain variables at this stage
         actual_user = template.template(self.basedir, actual_user, inject)


Test patch: