diff --git a/changelogs/fragments/win_copy-preserve-tmp-folder.yaml b/changelogs/fragments/win_copy-preserve-tmp-folder.yaml new file mode 100644 index 00000000000..ea6a93e2af0 --- /dev/null +++ b/changelogs/fragments/win_copy-preserve-tmp-folder.yaml @@ -0,0 +1,3 @@ +bugfixes: +- win_copy - Preserve the local tmp folder instead of deleting it so future tasks can use it + https://github.com/ansible/ansible/pull/37964 diff --git a/lib/ansible/plugins/action/win_copy.py b/lib/ansible/plugins/action/win_copy.py index 3b4616c7551..0566b95940c 100644 --- a/lib/ansible/plugins/action/win_copy.py +++ b/lib/ansible/plugins/action/win_copy.py @@ -11,6 +11,7 @@ import base64 import json import os import os.path +import shutil import tempfile import traceback import zipfile @@ -320,12 +321,10 @@ class ActionModule(ActionBase): ) ) copy_args.pop('content', None) - os.remove(zip_path) - module_return = self._execute_module(module_name='copy', module_args=copy_args, task_vars=task_vars) - os.removedirs(os.path.dirname(zip_path)) + shutil.rmtree(os.path.dirname(zip_path)) return module_return def run(self, tmp=None, task_vars=None): diff --git a/test/integration/targets/win_copy/tasks/main.yml b/test/integration/targets/win_copy/tasks/main.yml index 0e25c514ca0..b2ee103fd06 100644 --- a/test/integration/targets/win_copy/tasks/main.yml +++ b/test/integration/targets/win_copy/tasks/main.yml @@ -5,6 +5,16 @@ state: directory delegate_to: localhost +# removes the cached zip module from the previous task so we can replicate +# the below issue where win_copy would delete DEFAULT_LOCAL_TMP if it +# had permission to +# https://github.com/ansible/ansible/issues/35613 +- name: clear the local ansiballz cache + file: + path: "{{lookup('config', 'DEFAULT_LOCAL_TMP')}}/ansiballz_cache" + state: absent + delegate_to: localhost + - name: create test folder win_file: path: '{{test_win_copy_path}}'