Repository: ansible/ansible

Issue:
remote_tmp filling up when using the shell module with the async option ( or [fixed] using copy module on remote host )
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
remote_tmp

##### ANSIBLE VERSION
```
ansible 2.1.2.0
```

##### CONFIGURATION
[defaults]

hostfile       = /etc/ansible/hosts
library        = /usr/share/ansible
remote_tmp     = $HOME/.ansible/tmp
pattern        = *
forks          = 5
poll_interval  = 15
transport      = smart
remote_port    = 22
module_lang    = C
callback_plugins = /root/foryou_ansible/plugins/callback
callback_plugins = /home/mkhaskhoussi/ansible_4YOU/plugins/callback
callback_plugins = /home/foryou/ansible_4YOU_BAMBOO/plugins/callback
bin_ansible_callbacks = True
gathering = implicit
timeout =120
executable = /bin/sh
jinja2_extensions = jinja2.ext.do,jinja2.ext.i18n
display_skipped_hosts = True
error_on_undefined_vars = False
system_warnings = True
deprecation_warnings = True
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
[accelerate]
accelerate_port = 5099
accelerate_timeout = 30
accelerate_connect_timeout = 5.0

##### OS / ENVIRONMENT
CentOS Linux release 7.2.1511 

##### SUMMARY
When running my playbook to deploy on a host, the $HOME/.ansible/tmp directory is filled of directories containing a copy file and the $HOME/.ansible_async directory is filled with temporary files containing asynchronous information. The $HOME directory is more than 3Go, how come these temporary directories are not cleaned up ?

##### STEPS TO REPRODUCE
Run a copy module with the remote_src option and about async, run a shell module with the async option:
```
 - hosts: all
   tasks:
   - name: Copy files from etc  directory
     copy:
       remote_src: True
       src: /dv19/txt/tmp/TOPER.758
       dest: /dv19/txt/tmp/ansible_TOPER
     ignore_errors: false

   - name: start karaf
     shell: chdir=/dv19/txt/tmp/ ls ansible_TOPER
     async: 30
     poll: 29

```

##### EXPECTED RESULTS
I expect the temporary directories and files to be removed from the host $HOME directory

##### ACTUAL RESULTS
After running the playbook:
```
Using /etc/ansible/ansible.cfg as config file
Loaded callback default of type stdout, v2.0

PLAYBOOK: test_tmp.yml *********************************************************
1 plays in test_tmp.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'( umask 77 &                                                                                        & mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484137977.71-171235108886646                                                                                         `" && echo ansible-tmp-1484137977.71-171235108886646="` echo $HOME/.ansible/tmp/                                                                                        ansible-tmp-1484137977.71-171235108886646 `" ) && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> PUT /tmp/tmpjk4hoF TO /home/dv19/.ansible/tmp/ansib                                                                                        le-tmp-1484137977.71-171235108886646/setup
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -                                                                                        o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredA                                                                                        uthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthe                                                                                        ntication=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.an                                                                                        sible/cp/ansible-ssh-%h-%p-%r '[slnxhrardfr02.ptx.fr.sopra]'
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'chmod u+x /h                                                                                        ome/dv19/.ansible/tmp/ansible-tmp-1484137977.71-171235108886646/ /home/dv19/.ans                                                                                        ible/tmp/ansible-tmp-1484137977.71-171235108886646/setup && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r -tt slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'LANG=C L                                                                                        C_ALL=C http_proxy=http://manh.proxy.corp.sopra:8080 LC_MESSAGES=C https_proxy=h                                                                                        ttp://manh.proxy.corp.sopra:8080 /usr/bin/python /home/dv19/.ansible/tmp/ansible                                                                                        -tmp-1484137977.71-171235108886646/setup; rm -rf "/home/dv19/.ansible/tmp/ansibl                                                                                        e-tmp-1484137977.71-171235108886646/" > /dev/null 2>&1 && sleep 0'"'"''
ok: [web2]

TASK [Copy files from etc  directory] ******************************************
task path: /home/fvaltat/install_webtools/test_tmp.yml:4
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'( umask 77 &                                                                                        & mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484137978.92-62443342011669 `                                                                                        " && echo ansible-tmp-1484137978.92-62443342011669="` echo $HOME/.ansible/tmp/an                                                                                        sible-tmp-1484137978.92-62443342011669 `" ) && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> PUT /tmp/tmpy6cXRt TO /home/dv19/.ansible/tmp/ansib                                                                                        le-tmp-1484137978.92-62443342011669/copy
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -                                                                                        o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredA                                                                                        uthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthe                                                                                        ntication=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.an                                                                                        sible/cp/ansible-ssh-%h-%p-%r '[slnxhrardfr02.ptx.fr.sopra]'
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'chmod u+x /h                                                                                        ome/dv19/.ansible/tmp/ansible-tmp-1484137978.92-62443342011669/ /home/dv19/.ansi                                                                                        ble/tmp/ansible-tmp-1484137978.92-62443342011669/copy && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r -tt slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'LANG=C L                                                                                        C_ALL=C http_proxy=http://manh.proxy.corp.sopra:8080 LC_MESSAGES=C https_proxy=h                                                                                        ttp://manh.proxy.corp.sopra:8080 /usr/bin/python /home/dv19/.ansible/tmp/ansible                                                                                        -tmp-1484137978.92-62443342011669/copy && sleep 0'"'"''
ok: [web2] => {"changed": false, "checksum": "4c33eda1935b9fb4f993d98c9377241ce2                                                                                        943fee", "dest": "/dv19/txt/tmp/ansible_TOPER", "gid": 54323, "group": "hr", "in                                                                                        vocation": {"module_args": {"backup": false, "content": null, "delimiter": null,                                                                                         "dest": "/dv19/txt/tmp/ansible_TOPER", "directory_mode": null, "follow": false,                                                                                         "force": true, "group": null, "mode": null, "original_basename": null, "owner":                                                                                         null, "regexp": null, "remote_src": true, "selevel": null, "serole": null, "set                                                                                        ype": null, "seuser": null, "src": "/dv19/txt/tmp/TOPER.758", "validate": null}}                                                                                        , "md5sum": "2af16f8f3eb9f02bfc1a3f52848d7e5a", "mode": "0644", "owner": "dv19",                                                                                         "size": 3727, "src": "/dv19/txt/tmp/TOPER.758", "state": "file", "uid": 54324}

TASK [start karaf] *************************************************************
task path: /home/fvaltat/install_webtools/test_tmp.yml:11
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'( umask 77 &                                                                                        & mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484137979.41-73481556046878 `                                                                                        " && echo ansible-tmp-1484137979.41-73481556046878="` echo $HOME/.ansible/tmp/an                                                                                        sible-tmp-1484137979.41-73481556046878 `" ) && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> PUT /tmp/tmp_RwBbR TO /home/dv19/.ansible/tmp/ansib                                                                                        le-tmp-1484137979.41-73481556046878/command
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -                                                                                        o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredA                                                                                        uthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthe                                                                                        ntication=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.an                                                                                        sible/cp/ansible-ssh-%h-%p-%r '[slnxhrardfr02.ptx.fr.sopra]'
<slnxhrardfr02.ptx.fr.sopra> PUT /tmp/tmpkiQdsX TO /home/dv19/.ansible/tmp/ansib                                                                                        le-tmp-1484137979.41-73481556046878/async_wrapper
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -                                                                                        o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredA                                                                                        uthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthe                                                                                        ntication=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.an                                                                                        sible/cp/ansible-ssh-%h-%p-%r '[slnxhrardfr02.ptx.fr.sopra]'
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'chmod u+x /h                                                                                        ome/dv19/.ansible/tmp/ansible-tmp-1484137979.41-73481556046878/ /home/dv19/.ansi                                                                                        ble/tmp/ansible-tmp-1484137979.41-73481556046878/command /home/dv19/.ansible/tmp                                                                                        /ansible-tmp-1484137979.41-73481556046878/async_wrapper && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r -tt slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'LANG=C L                                                                                        C_ALL=C http_proxy=http://manh.proxy.corp.sopra:8080 LC_MESSAGES=C https_proxy=h                                                                                        ttp://manh.proxy.corp.sopra:8080 /home/dv19/.ansible/tmp/ansible-tmp-1484137979.                                                                                        41-73481556046878/async_wrapper 564209830796 30 /home/dv19/.ansible/tmp/ansible-                                                                                        tmp-1484137979.41-73481556046878/command && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'rm -f -r /ho                                                                                        me/dv19/.ansible/tmp/ansible-tmp-1484137979.41-73481556046878/ > /dev/null 2>&1                                                                                         && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'( umask 77 &                                                                                        & mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1484138010.13-119316880379818                                                                                         `" && echo ansible-tmp-1484138010.13-119316880379818="` echo $HOME/.ansible/tmp/                                                                                        ansible-tmp-1484138010.13-119316880379818 `" ) && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> PUT /tmp/tmpGzpdwx TO /home/dv19/.ansible/tmp/ansib                                                                                        le-tmp-1484138010.13-119316880379818/async_status
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -                                                                                        o ControlPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredA                                                                                        uthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthe                                                                                        ntication=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.an                                                                                        sible/cp/ansible-ssh-%h-%p-%r '[slnxhrardfr02.ptx.fr.sopra]'
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'chmod u+x /h                                                                                        ome/dv19/.ansible/tmp/ansible-tmp-1484138010.13-119316880379818/ /home/dv19/.ans                                                                                        ible/tmp/ansible-tmp-1484138010.13-119316880379818/async_status && sleep 0'"'"''
<slnxhrardfr02.ptx.fr.sopra> ESTABLISH SSH CONNECTION FOR USER: dv19
<slnxhrardfr02.ptx.fr.sopra> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o Cont                                                                                        rolPersist=60s -o Port=22 -o KbdInteractiveAuthentication=no -o PreferredAuthent                                                                                        ications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthenticat                                                                                        ion=no -o User=dv19 -o ConnectTimeout=120 -o ControlPath=/home/fvaltat/.ansible/                                                                                        cp/ansible-ssh-%h-%p-%r -tt slnxhrardfr02.ptx.fr.sopra '/bin/sh -c '"'"'LANG=C L                                                                                        C_ALL=C LC_MESSAGES=C /usr/bin/python /home/dv19/.ansible/tmp/ansible-tmp-148413                                                                                        8010.13-119316880379818/async_status; rm -rf "/home/dv19/.ansible/tmp/ansible-tm                                                                                        p-1484138010.13-119316880379818/" > /dev/null 2>&1 && sleep 0'"'"''
changed: [web2] => {"ansible_job_id": "564209830796.26301", "changed": true, "cm                                                                                        d": "ls ansible_TOPER", "delta": "0:00:00.008058", "end": "2017-01-11 13:33:01.0                                                                                        51367", "finished": 1, "invocation": {"module_args": {"_raw_params": "ls ansible                                                                                        _TOPER", "_uses_shell": true, "chdir": "/dv19/txt/tmp/", "creates": null, "execu                                                                                        table": null, "removes": null, "warn": true}, "module_name": "async_status"}, "r                                                                                        c": 0, "start": "2017-01-11 13:33:01.043309", "stderr": "", "stdout": "ansible_T                                                                                        OPER", "stdout_lines": ["ansible_TOPER"], "warnings": []}

PLAY RECAP *********************************************************************
web2                       : ok=3    changed=1    unreachable=0    failed=0

```
on the host, in the $HOME/.ansible/tmp directory, a  ansible-tmp-1484137978.92-62443342011669 directory still exists and contains a copy file: cat copy:
```
#!/usr/bin/python
# -*- coding: utf-8 -*-
ZIPLOADER_WRAPPER = True # For test-module script to tell this is a ZIPLOADER_WRAPPER
import os
import sys
import base64
import shutil
import zipfile
import tempfile
import subprocess
if sys.version_info < (3,):
    bytes = str
    PY3 = False
else:
    unicode = str
    PY3 = True
try:
    from io import BytesIO as IOStream
except ImportError:
.......
```

on the host, in the $HOME/.ansible_async directory, a  564209830796.26301 file still exists and contains: cat 564209830796.26301:
```
{"changed": true, "end": "2017-01-11 13:33:01.051367", "stdout": "ansible_TOPER", "cmd": "ls ansible_TOPER", "start": "2017-01-11 13:33:01.043309", "delta": "0:00:00.008058", "stderr": "", "rc": 0, "invocation": {"module_args": {"creates": null, "executable": null, "_uses_shell": true, "_raw_params": "ls ansible_TOPER", "removes": null, "warn": true, "chdir": "/dv19/txt/tmp/"}}, "warnings": []}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/plugins/action/assemble.py b/lib/ansible/plugins/action/assemble.py
--- a/lib/ansible/plugins/action/assemble.py
+++ b/lib/ansible/plugins/action/assemble.py
@@ -103,13 +103,8 @@ def run(self, tmp=None, task_vars=None):
             return result
 
         remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
-        if not tmp:
-            tmp = self._make_tmp_path(remote_user)
-            self._cleanup_remote_tmp = True
-
         if boolean(remote_src):
-            result.update(self._execute_module(tmp=tmp, task_vars=task_vars, delete_remote_tmp=False))
-            self._remove_tmp_path(tmp)
+            result.update(self._execute_module(tmp=tmp, task_vars=task_vars))
             return result
         else:
             try:
@@ -119,6 +114,10 @@ def run(self, tmp=None, task_vars=None):
                 result['msg'] = to_native(e)
                 return result
 
+        if not tmp:
+            tmp = self._make_tmp_path(remote_user)
+            self._cleanup_remote_tmp = True
+
         if not os.path.isdir(src):
             result['failed'] = True
             result['msg'] = u"Source (%s) is not a directory" % src
diff --git a/lib/ansible/plugins/action/copy.py b/lib/ansible/plugins/action/copy.py
--- a/lib/ansible/plugins/action/copy.py
+++ b/lib/ansible/plugins/action/copy.py
@@ -87,7 +87,7 @@ def run(self, tmp=None, task_vars=None):
         # if we have first_available_file in our vars
         # look up the files and use the first one we find as src
         elif remote_src:
-            result.update(self._execute_module(module_name='copy', module_args=self._task.args, task_vars=task_vars, delete_remote_tmp=False))
+            result.update(self._execute_module(module_name='copy', module_args=self._task.args, task_vars=task_vars))
             return result
         else:  # find in expected paths
             try:


Test patch: