From a6877664fa0939124bad88c504c93dc2b43b81f6 Mon Sep 17 00:00:00 2001 From: s-hertel <19572925+s-hertel@users.noreply.github.com> Date: Thu, 14 Nov 2024 13:29:52 -0500 Subject: [PATCH] restore using set_group_attributes_if_different and set_owner_attributes_if_different set_context_if_different doesn't handle context=None as a no-op, reconsider using set_fs_attributes_if_different later once path recursion is consolidated --- lib/ansible/modules/copy.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/ansible/modules/copy.py b/lib/ansible/modules/copy.py index 9c9f674f811..da458732a2c 100644 --- a/lib/ansible/modules/copy.py +++ b/lib/ansible/modules/copy.py @@ -333,23 +333,24 @@ def adjust_recursive_directory_permissions(pre_existing_dir, new_directory_list, return changed +def chown_path(module, path, owner, group): + """Update the owner/group if specified and different from the current owner/group.""" + changed = module.set_owner_if_different(path, owner, False) + return module.set_group_if_different(path, group, changed) + + def chown_recursive(path, module): changed = False owner = module.params['owner'] group = module.params['group'] - # TODO: Consolidate with the other methods calling set_*_if_different methods. - # Recursing the path for every attribute like this, is inefficient. - file_args = {'owner': owner, 'group': group, 'secontext': None, 'mode': None, 'attributes': None} + # TODO: Consolidate with the other methods calling set_*_if_different method, this is inefficient. for dirpath, dirnames, filenames in os.walk(path): - file_args['path'] = dirpath - changed = module.set_fs_attributes_if_different(file_args, changed) + changed |= chown_path(module, dirpath, owner, group) for subdir in [os.path.join(dirpath, d) for d in dirnames]: - file_args['path'] = subdir - changed = module.set_fs_attributes_if_different(file_args, changed) + changed |= chown_path(module, subdir, owner, group) for filepath in [os.path.join(dirpath, f) for f in filenames]: - file_args['path'] = filepath - changed = module.set_fs_attributes_if_different(file_args, changed) + changed |= chown_path(module, filepath, owner, group) return changed