Repository: ansible/ansible

Issue:
"Temporary" commit breaks ANSIBLE_KEEP_REMOTE_FILES for 'localhost'
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
`lib/ansible/plugins/action/__init__.py`

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.5.0 (devel 40f7f4babf) last updated 2018/02/05 21:35:15 (GMT +1000)
  config file = None
  configured module search path = [u'/Users/will/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/will/src/ansible/lib/ansible
  executable location = /Users/will/src/ansible/bin/ansible
  python version = 2.7.14 (default, Jan  6 2018, 12:16:16) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
```

##### CONFIGURATION
ANSIBLE_KEEP_REMOTE_FILES=1

##### OS / ENVIRONMENT
I assume this isn't OS/python related, but in case it is, I spotted this using brew python on OS X

##### SUMMARY
Since bbd6b8bb42cc8fa48, ANSIBLE_KEEP_REMOTE_FILES no longer works.

##### STEPS TO REPRODUCE
Run any playbook with `ANSIBLE_KEEP_REMOTE_FILES=1` set, along with `-vvv`, and then try and explode the resulting module files.

<!--- Paste example playbooks or commands between quotes below -->
```yaml

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
```
$ /usr/local/opt/python/bin/python2.7 /Users/will/.ansible/tmp/ansible-tmp-1517830651.23-79915697239471/stat.py explode
Module expanded into:
/Users/will/.ansible/tmp/ansible-tmp-1517830651.23-79915697239471/debug_dir
```

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
$ /usr/local/opt/python/bin/python2.7 /Users/will/.ansible/tmp/ansible-local-15212xlCfsb/ansible-tmp-1517830723.82-13636234799604/stat.py explode
/usr/local/Cellar/python/2.7.14_2/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file '/Users/will/.ansible/tmp/ansible-local-15212xlCfsb/ansible-tmp-1517830723.82-13636234799604/stat.py': [Errno 2] No such file or directory
```



Discussion:
cc @Qalthos @ganeshrn @gundalow @jmighion @kedarX @ogenstad @paulquack @privateip @rcarrillocruz @samerd @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I cannot reproduce:
```
 ANSIBLE_KEEP_REMOTE_FILES=1 ansible -m stat -a 'path=/etc/hosts' -vvvv testhost
```
from output:
```
<127.0.0.3> 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/bcoca/.ansible/cp/047663f5ee 127.0.0.3 '/bin/sh -c '"'"'chmod u+x /home/bcoca/.ansible/tmp/ansible-tmp-1517841571.0417922-162025592802770/ /home/bcoca/.ansible/tmp/ansible-tmp-1517841571.0417922-162025592802770/stat.py && sleep 0'"'"''  
```
and then:

```
#>stat /home/bcoca/.ansible/tmp/ansible-tmp-1517841571.0417922-162025592802770/stat.py
  File: /home/bcoca/.ansible/tmp/ansible-tmp-1517841571.0417922-162025592802770/stat.py
  Size: 76498           Blocks: 152        IO Block: 4096   regular file
Device: 803h/2051d      Inode: 9701360     Links: 1
Access: (0700/-rwx------)  Uid: ( 1000/   bcoca)   Gid: ( 1000/   bcoca)
Access: 2018-02-05 09:39:32.190852388 -0500
Modify: 2018-02-05 09:39:32.193852388 -0500
Change: 2018-02-05 09:39:32.201852387 -0500
 Birth: -
```
And when I try w/o the setting, there is no tmp dir.
actually I found the opposite bug, remote temp files never get removed in devel. 389f4ef1fb71629a3389306e6324ea5b118275ff
Glad I helped find a (different) bug! I suspect this must be OS X related somehow if you can't reproduce it - I'd have thought I'd have noticed this earlier if it was on all platforms, but OS X is my hobby development environment.

I bisected where it breaks to that very commit, so it's definitely related, but I'll have to investigate this harder later as it's clearly an edge case of some kind.
so reverting this #35733, fixes it for me on other platforms, don't have OS X but it seems really wrong if so.
It seems that the temp files are now cleaned up on OS X when ansible or ansible-playbook finishes, and not when the task finishes.
bin/ansible runs `shutil.rmtree(C.DEFAULT_LOCAL_TMP)` at the end - it's this that cleans up the remote files.

I think before the change in question, the remote files directory was under something other than `C.DEFAULT_LOCAL_TMP`

Before: ` /Users/will/.ansible/tmp/ansible-tmp-1517917593.4-271581342652258/stat.py`

After: `/Users/will/.ansible/tmp/ansible-local-174741XInKU/ansible-tmp-1517917518.3-107087668300061/stat.py`

where I think `C.DEFAULT_LOCAL_TMP` would be something like `/Users/will/.ansible/tmp/ansible-local-174741XInKU` in both cases, so `shutil.rmtree` would delete the latter but not the former.

The only real question other than how to fix this is why does this *not* happen on Linux?

so those are 'local files' not really remote, which has been removed on controller (or attempted to) for a while now.

My tests were with 'remotes', let my try using local to see if there is some short circuit that is imposing the 'local tmp' setting vs the 'remote tmp' setting.

Resolving patch:
diff --git a/lib/ansible/inventory/data.py b/lib/ansible/inventory/data.py
--- a/lib/ansible/inventory/data.py
+++ b/lib/ansible/inventory/data.py
@@ -97,7 +97,6 @@ def _create_implicit_localhost(self, pattern):
                                 'You can correct this by setting ansible_python_interpreter for localhost')
             new_host.set_variable("ansible_python_interpreter", py_interp)
             new_host.set_variable("ansible_connection", 'local')
-            new_host.set_variable("ansible_remote_tmp", C.DEFAULT_LOCAL_TMP)
 
             self.localhost = new_host
 


Test patch: