From 6ab97c0d725d32864b08f96de9936b6193b38be4 Mon Sep 17 00:00:00 2001 From: pinrada Date: Sat, 20 Apr 2024 00:14:44 -0400 Subject: [PATCH 1/3] fixing bug79587 --- lib/ansible/modules/file.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py index 564d7f6cdbe0bc..088701537cb64b 100644 --- a/lib/ansible/modules/file.py +++ b/lib/ansible/modules/file.py @@ -6,7 +6,6 @@ from __future__ import annotations - DOCUMENTATION = r''' --- module: file @@ -214,6 +213,12 @@ path: /etc/foo state: absent +- name: Remove file attributes and recursively remove directory + ansible.builtin.file: + path: /etc/foo + state: absent + attribute: -a + ''' RETURN = r''' dest: @@ -521,16 +526,29 @@ def ensure_absent(path): b_path = to_bytes(path, errors='surrogate_or_strict') prev_state = get_state(b_path) result = {} + file_args = module.load_file_common_arguments(module.params) + changed = False if prev_state != 'absent': diff = initial_diff(path, 'absent', prev_state) if not module.check_mode: if prev_state == 'directory': + # Check if 'attributes' in file_args + current_attributes = file_args.get('attributes', '') + if '-a' in current_attributes: + # Attempt to remove the append-only attribute before deletion + try: + module.set_attributes_if_different(path, current_attributes, changed, diff, expand=False) + changed = True # Mark as changed if attributes were modified + except Exception as e: + module.fail_json(msg="Failed to change attributes: %s" % to_native(e), path=to_native(b_path)) try: shutil.rmtree(b_path, ignore_errors=False) except Exception as e: - raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e)}) + # raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e)}) + raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e), + 'details': "Attributes before removal: %s, Changed: %s" % (file_args.get('attributes', 'None'), changed)}) else: try: os.unlink(b_path) From ed040fb6a74758df3d18684f2193088c2007ddce Mon Sep 17 00:00:00 2001 From: pinrada Date: Sat, 20 Apr 2024 00:50:16 -0400 Subject: [PATCH 2/3] Fixes #79587: Adds a call to function set_attributes_if_different to ensure_absent --- lib/ansible/modules/file.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py index 088701537cb64b..c29d3dfe4dcfc6 100644 --- a/lib/ansible/modules/file.py +++ b/lib/ansible/modules/file.py @@ -6,6 +6,7 @@ from __future__ import annotations + DOCUMENTATION = r''' --- module: file @@ -539,16 +540,16 @@ def ensure_absent(path): if '-a' in current_attributes: # Attempt to remove the append-only attribute before deletion try: - module.set_attributes_if_different(path, current_attributes, changed, diff, expand=False) + module.set_attributes_if_different(path, current_attributes, changed, + diff, expand=False) changed = True # Mark as changed if attributes were modified except Exception as e: - module.fail_json(msg="Failed to change attributes: %s" % to_native(e), path=to_native(b_path)) + module.fail_json(msg="Failed to change attributes: %s" % to_native(e), + path=to_native(b_path)) try: shutil.rmtree(b_path, ignore_errors=False) except Exception as e: - # raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e)}) - raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e), - 'details': "Attributes before removal: %s, Changed: %s" % (file_args.get('attributes', 'None'), changed)}) + raise AnsibleModuleError(results={'msg': "rmtree failed: %s" % to_native(e)}) else: try: os.unlink(b_path) From c1e692934d77319ac347b83045f7d43fc5171242 Mon Sep 17 00:00:00 2001 From: pinrada Date: Sat, 20 Apr 2024 01:19:34 -0400 Subject: [PATCH 3/3] Fixes #79587: Improve style --- lib/ansible/modules/file.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/file.py b/lib/ansible/modules/file.py index c29d3dfe4dcfc6..007809ec186212 100644 --- a/lib/ansible/modules/file.py +++ b/lib/ansible/modules/file.py @@ -528,24 +528,24 @@ def ensure_absent(path): prev_state = get_state(b_path) result = {} file_args = module.load_file_common_arguments(module.params) - changed = False + changed = False if prev_state != 'absent': diff = initial_diff(path, 'absent', prev_state) if not module.check_mode: if prev_state == 'directory': - # Check if 'attributes' in file_args + # Check if 'attributes' in file_args current_attributes = file_args.get('attributes', '') if '-a' in current_attributes: # Attempt to remove the append-only attribute before deletion try: - module.set_attributes_if_different(path, current_attributes, changed, + module.set_attributes_if_different(path, current_attributes, changed, diff, expand=False) changed = True # Mark as changed if attributes were modified except Exception as e: - module.fail_json(msg="Failed to change attributes: %s" % to_native(e), - path=to_native(b_path)) + module.fail_json(msg="Failed to change attributes: %s" % to_native(e), + path=to_native(b_path)) try: shutil.rmtree(b_path, ignore_errors=False) except Exception as e: