From 9514a618b71552df7bcc1b37ad0c0e59e17d88e5 Mon Sep 17 00:00:00 2001 From: Nikita Chepanov Date: Mon, 14 May 2018 16:17:38 -0400 Subject: [PATCH] Add copy module support for filesystems without chmod (#40099) * only relevant to remote_src=True * separate file content copying from file stats copying * raise exception on mode='preserve', ignore otherwise Fixes: #19731 Signed-off-by: Nikita Chepanov --- lib/ansible/modules/files/copy.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/files/copy.py b/lib/ansible/modules/files/copy.py index b36eb854eb..8bce968b4a 100644 --- a/lib/ansible/modules/files/copy.py +++ b/lib/ansible/modules/files/copy.py @@ -233,6 +233,7 @@ import os import os.path import shutil import stat +import errno import tempfile import traceback @@ -408,9 +409,17 @@ def main(): b_mysrc = b_src if remote_src: _, b_mysrc = tempfile.mkstemp(dir=os.path.dirname(b_dest)) - shutil.copy2(b_src, b_mysrc) + + shutil.copyfile(b_src, b_mysrc) + try: + shutil.copystat(b_src, b_mysrc) + except OSError as err: + if err.errno == errno.ENOSYS and mode == "preserve": + module.warn("Unable to copy stats {0}".format(to_native(b_src))) + else: + raise module.atomic_move(b_mysrc, dest, unsafe_writes=module.params['unsafe_writes']) - except IOError: + except (IOError, OSError): module.fail_json(msg="failed to copy: %s to %s" % (src, dest), traceback=traceback.format_exc()) changed = True else: