Repository: ansible/ansible

Issue:
Async task loses its status when using non-standard async dir
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
core

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.1
  config file = /home/zverik/progr/git/ansible-tile/ansible.cfg
  configured module search path = [u'/home/zverik/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
Running on Fedora 27, managing Ubuntu 16.04.

##### SUMMARY
<!--- Explain the problem briefly -->
When a non-standard directory for `ansible_async` is used via `ANSIBLE_ASYNC_DIR`, the pid file inside it is lost, because `async_status` module does not receive the variable.

This env variable was introduced in #31677, but it seems it wasn't tested.

##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- hosts: all
  tasks:
    - name: Test async
      command: sleep 10
      async: 15
      environment:
        ANSIBLE_ASYNC_DIR: /tmp/ansible_async
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
One changed task. The same as without the `environment` section.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
The task fails with "could not find job". The file is present in /tmp/ansible_async and contains correct data.

<!--- Paste verbatim command output between quotes below -->
```
fatal: [174.138.6.248]: FAILED! => {
    "ansible_job_id": "493455192495.16696", 
    "changed": false, 
    "finished": 1, 
    "invocation": {
        "module_args": {
            "jid": "493455192495.16696", 
            "mode": "status"
        }
    }, 
    "msg": "could not find job", 
    "started": 1
}
```

In the `-vvvv` log I see a couple commands that might help:

SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/zverik/.ansible/cp/b422f3b18f -tt 174.138.6.248 '/bin/sh -c '"'"'ANSIBLE_ASYNC_DIR=/tmp/ansible_async /usr/bin/python /home/zverik/.ansible/tmp/ansible-tmp-1525377488.25-39354984611739/async_wrapper.py 493455192495 15 /home/zverik/.ansible/tmp/ansible-tmp-1525377488.25-39354984611739/command.py _ && sleep 0'"'"''

...

SSH: EXEC ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/home/zverik/.ansible/cp/b422f3b18f -tt 174.138.6.248 '/bin/sh -c '"'"'/usr/bin/python /home/zverik/.ansible/tmp/ansible-tmp-1525377500.85-203198047209178/async_status.py && sleep 0'"'"''


Discussion:
Unsure if more than this is required, but this small change seems to resolve this:

```diff
diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
index 4924461f94..b487771ac1 100644
--- a/lib/ansible/executor/task_executor.py
+++ b/lib/ansible/executor/task_executor.py
@@ -713,7 +713,7 @@ class TaskExecutor:
         # that (with a sleep for "poll" seconds between each retry) until the
         # async time limit is exceeded.

-        async_task = Task().load(dict(action='async_status jid=%s' % async_jid))
+        async_task = Task().load(dict(action='async_status jid=%s' % async_jid, environment=self._task.environment))

         # FIXME: this is no longer the case, normal takes care of all, see if this can just be generalized
         # Because this is an async task, the action handler is async. However,
```

cc @bcoca 


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
@@ -703,7 +703,7 @@ def _poll_async_result(self, result, templar, task_vars=None):
         # that (with a sleep for "poll" seconds between each retry) until the
         # async time limit is exceeded.
 
-        async_task = Task().load(dict(action='async_status jid=%s' % async_jid))
+        async_task = Task().load(dict(action='async_status jid=%s' % async_jid, environment=self._task.environment))
 
         # FIXME: this is no longer the case, normal takes care of all, see if this can just be generalized
         # Because this is an async task, the action handler is async. However,


Test patch: