Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file module fix attribute issues #83111

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/ansible/modules/file.py
Expand Up @@ -214,6 +214,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:
Expand Down Expand Up @@ -521,12 +527,25 @@ 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a case when current_attributes is None:

Traceback (most recent call last):
  File "<stdin>", line 133, in <module>
  File "<stdin>", line 125, in _ansiballz_main
  File "<stdin>", line 73, in invoke_module
  File "/usr/local/lib/python3.9/runpy.py", line 225, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/local/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/local/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_file_payload_unz_zxje/ansible_file_payload.zip/ansible/modules/file.py", line 1007, in <module>
  File "/tmp/ansible_file_payload_unz_zxje/ansible_file_payload.zip/ansible/modules/file.py", line 1001, in main
  File "/tmp/ansible_file_payload_unz_zxje/ansible_file_payload.zip/ansible/modules/file.py", line 540, in ensure_absent
TypeError: argument of type 'NoneType' is not iterable

(https://dev.azure.com/ansible/ansible/_build/results?buildId=109805&view=logs&j=c9dc1718-7fc3-5e87-e6a6-571058301006&t=ff7b63cb-2767-53c3-7382-bd37bd9a7b6a&l=2143)

# 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:
Expand Down