Repository: ansible/ansible

Issue:
async_status file not using ansible_python_interpreter (was: Ansible 2.0.0.2 async polling is not working)
This is with PCBSD 10.2 system running Ansible to configure a digital ocean FreeBSD 10.2 droplet.

This is the task:

```
- name: Just testing
  command: sleep 120
  async: 7200
  poll: 3
```

The problems are:
- With the poll present; the process stops after only 10 seconds with the error: "async task did not complete within the requested time".
- With poll line removed ;then no error and the playbook continues after approximately 4 seconds and completes without error.

In both cases logging in with ssh to the droplet shows the sleep 120 is still running, and once complete the a zero size file in ".ansible_async" gets the results added to it (the results file looks ok).

Notes:
- The intsaledl Ansible 2.002 has `plugins/action/__init__.py` from devel branch to allow csh to work.
- I also added user with zsh which gave exactly the same results.



Discussion:
May you try this again with a non-modified version of Ansible and verbose output (`-vvv`)?

This seems to be the problem:

```
SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=sysop -o ConnectTimeout=10 -o ControlPath=/usr/home/me/.ansible/cp/ansible-ssh-%h-%p-%r -tt fbsd.mydom.tld 'LANG=en_GB.UTF-8 LC_ALL=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 /usr/local/bin/python /home/sysop/.ansible/tmp/ansible-tmp-1453779722.75-25121660077230/async_status; rm -rf "/home/sysop/.ansible/tmp/ansible-tmp-1453779722.75-25121660077230/" > /dev/null 2>&1'
```

Ansible seems to be using `/usr/local/bin/python`  to run the async status; even though I have `ansible_python_interpreter=/usr/local/bin/python2.7`.
(I tried adding it to ansible-playbook with '-e' option, but still same command is issued.

I checked the remote temp files: (seem to use ansible_python_interpreter correctly)

```
$ grep -r 'bin/python' .ansible*
.ansible/tmp/ansible-tmp-1453785481.97-100679098860532/setup:#!/usr/local/bin/python2.7 
.ansible/tmp/ansible-tmp-1453785485.25-98319990319062/command:#!/usr/local/bin/python2.7 
.ansible/tmp/ansible-tmp-1453785485.25-98319990319062/async_wrapper:#!/usr/local/bin/python2.7 
```

Python version: (ansible commands all have `#!/usr/local/bin/python2.7`)

```
% python2.7
Python 2.7.11 (default, Dec 23 2015, 10:13:16) 
[GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 (tags/RELEASE_34/dot1-final 208032)] on freebsd10
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; print sys.executable
/usr/local/bin/python2.7
```

Notes:
- Temporarily adding appropriate symlink allows polled command to run, but it should really use `ansible_python_interpreter` for this.
- The no "poll:" case behaves as "poll: 0" i.e. start the command for later testing.  I though I had read that the default was 10 seconds somewhere - I was probably mistaken.
- One other point without verbose mode there was no indication that polling was occurring, on 1.x there was a message after each "poll" seconds.

It is getting the value directly from: `modules/core/utilities/logic/async_status.py` which has `#!/usr/local/bin/python` and ignoring the `ansible_python_interpreter` value in this case.
(tested by modifying the #! line)

possibly related to: #13421 perhaps

Also seeing that async_status is not using the value for `ansible_python_interpreter`, and I am editing the title to reflect the root cause of your problem.  Not sure at this point if it is related to the issue in #13421.


Resolving patch:
diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
--- a/lib/ansible/executor/task_executor.py
+++ b/lib/ansible/executor/task_executor.py
@@ -468,7 +468,7 @@ def _execute(self, variables=None):
 
             if self._task.async > 0:
                 if self._task.poll > 0:
-                    result = self._poll_async_result(result=result, templar=templar)
+                    result = self._poll_async_result(result=result, templar=templar, task_vars=vars_copy)
 
                 # ensure no log is preserved
                 result["_ansible_no_log"] = self._play_context.no_log
@@ -552,11 +552,14 @@ def _evaluate_failed_when_result(result):
         display.debug("attempt loop complete, returning result")
         return result
 
-    def _poll_async_result(self, result, templar):
+    def _poll_async_result(self, result, templar, task_vars=None):
         '''
         Polls for the specified JID to be complete
         '''
 
+        if task_vars is None:
+            task_vars = self._job_vars
+
         async_jid = result.get('ansible_job_id')
         if async_jid is None:
             return dict(failed=True, msg="No job id was returned by the async task")
@@ -584,7 +587,7 @@ def _poll_async_result(self, result, templar):
         while time_left > 0:
             time.sleep(self._task.poll)
 
-            async_result = normal_handler.run()
+            async_result = normal_handler.run(task_vars=task_vars)
             # We do not bail out of the loop in cases where the failure
             # is associated with a parsing error. The async_runner can
             # have issues which result in a half-written/unparseable result


Test patch: