Update file.py's initial_diff() to list existing when state: absent (#56353)

pull/57549/head
Iain Madder 6 years ago committed by Sam Doran
parent fba67bbe53
commit 91d326cb68

@ -0,0 +1,2 @@
minor_changes:
- file - Extend ``-diff`` to return list of files and folders that will be removed in case of ``state=absent`` (https://github.com/ansible/ansible/pull/56353)

@ -367,6 +367,21 @@ def initial_diff(path, state, prev_state):
if prev_state != state:
diff['before']['state'] = prev_state
diff['after']['state'] = state
if state == 'absent':
walklist = {
'directories': [],
'files': [],
}
for base_path, sub_folders, files in os.walk(path):
for folder in sub_folders:
folderpath = os.path.join(base_path, folder)
walklist['directories'].append(folderpath)
for filename in files:
filepath = os.path.join(base_path, filename)
walklist['files'].append(filepath)
diff['before']['path_content'] = walklist
return diff
@ -481,6 +496,8 @@ def ensure_absent(path):
result = {}
if prev_state != 'absent':
diff = initial_diff(path, 'absent', prev_state)
if not module.check_mode:
if prev_state == 'directory':
try:
@ -495,7 +512,6 @@ def ensure_absent(path):
raise AnsibleModuleError(results={'msg': "unlinking failed: %s " % to_native(e),
'path': path})
diff = initial_diff(path, 'absent', prev_state)
result.update({'path': path, 'changed': True, 'diff': diff, 'state': 'absent'})
else:
result.update({'path': path, 'changed': False, 'state': 'absent'})

@ -243,7 +243,7 @@
when: selinux_installed.stdout != ""
ignore_errors: true
- name: remote directory foobar
- name: remove directory foobar
file: path={{output_dir}}/foobar state=absent
- name: remove file foo.txt
@ -258,6 +258,24 @@
- name: copy directory structure over
copy: src=foobar dest={{output_dir}}
- name: check what would be removed if folder state was absent and diff is enabled
file:
path: "{{ output_dir }}"
state: absent
check_mode: yes
diff: yes
register: folder_absent_result
- name: assert that the absent check lists expected files and folders
assert:
that:
- folder_absent_result.diff.before.path_content is defined
- test_folder in folder_absent_result.diff.before.path_content.directories
- test_file in folder_absent_result.diff.before.path_content.files
vars:
test_folder: "{{ folder_absent_result.path }}/foobar"
test_file: "{{ folder_absent_result.path }}/foobar/fileA"
- name: Change ownership of a directory with recurse=no(default)
file: path={{output_dir}}/foobar owner=1234

Loading…
Cancel
Save