summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDavid Shrewsbury <Shrews@users.noreply.github.com>2020-06-11 21:18:59 +0200
committerGitHub <noreply@github.com>2020-06-11 21:18:59 +0200
commit0ae4dac65a6f78dac4fcfce6d1074074eeac2f4e (patch)
treef71c4e33d4588d126c1e8fb2c989299044cbca4b /lib
parentFix changelog sanity test config detection. (diff)
downloadansible-0ae4dac65a6f78dac4fcfce6d1074074eeac2f4e.tar.xz
ansible-0ae4dac65a6f78dac4fcfce6d1074074eeac2f4e.zip
Fix copy module file perms with remote_src (#69993)
When using 'remote_src: yes' and 'mode: preserve', the code handling the file modes has to be handled on the remote node because it's the one that has access to the source files. This means that the copy module itself must handle this, rather than the copy action plugin (which is where all that logic exists). The copy module handles this when we copy a single file over. But when it is a directory as the src parameter value, the mode of the files beneath it are not considered. Subdirectories are copied with shutil.copytree() which will preserve permissions automatically. Individual files are copied with shutil.copyfile() which does NOT preserve permissions. We need to add some calls to shutil.copymode() to correct that. Note: This *always* retains individial file permissions. Specifying a 'mode' other than 'preserve' when giving a source directory for the 'src' param does not make sense so will be ignored in that case only. Fixes #69783 * Add changelog and test
Diffstat (limited to 'lib')
-rw-r--r--lib/ansible/modules/copy.py8
1 files changed, 8 insertions, 0 deletions
diff --git a/lib/ansible/modules/copy.py b/lib/ansible/modules/copy.py
index add01b056d..7f7844a217 100644
--- a/lib/ansible/modules/copy.py
+++ b/lib/ansible/modules/copy.py
@@ -395,6 +395,8 @@ def chown_recursive(path, module):
def copy_diff_files(src, dest, module):
+ """Copy files that are different between `src` directory and `dest` directory."""
+
changed = False
owner = module.params['owner']
group = module.params['group']
@@ -413,6 +415,7 @@ def copy_diff_files(src, dest, module):
os.symlink(linkto, b_dest_item_path)
else:
shutil.copyfile(b_src_item_path, b_dest_item_path)
+ shutil.copymode(b_src_item_path, b_dest_item_path)
if owner is not None:
module.set_owner_if_different(b_dest_item_path, owner, False)
@@ -423,6 +426,8 @@ def copy_diff_files(src, dest, module):
def copy_left_only(src, dest, module):
+ """Copy files that exist in `src` directory only to the `dest` directory."""
+
changed = False
owner = module.params['owner']
group = module.params['group']
@@ -458,6 +463,8 @@ def copy_left_only(src, dest, module):
if not os.path.islink(b_src_item_path) and os.path.isfile(b_src_item_path):
shutil.copyfile(b_src_item_path, b_dest_item_path)
+ shutil.copymode(b_src_item_path, b_dest_item_path)
+
if owner is not None:
module.set_owner_if_different(b_dest_item_path, owner, False)
if group is not None:
@@ -718,6 +725,7 @@ def main():
else:
changed = False
+ # If neither have checksums, both src and dest are directories.
if checksum_src is None and checksum_dest is None:
if remote_src and os.path.isdir(module.params['src']):
b_src = to_bytes(module.params['src'], errors='surrogate_or_strict')