Background: We have a “diagnostics” folder on our network that is used to store logs exported from our flagship application.
The problem I’m trying to solve is to remove any files/folders older than 30 days from the “diagnostics” folder.
The following code is being used to perform this:
name: Find all files older than 30 days in diagnostics folder
win_find:
paths: “{{ unc_path }}”
file_type: directory
age: 30d #recurse: yes
register: FilesOver30
name: Delete all files/folders older than 30 days from diagnostics folder
win_file:
path: “{{ unc_path }}”
state: absent
with_items: “{{ FilesOver30.files }}”
The issue I’m having is, if there are no files newer than 30 days in the “diagnostics” folder, it actually nukes the diagnostics folder itself, which is not the intended result.
Need to be able to have it remove any files/folders older than 30 days from this folder witout nuking the entire thing.
I’ve read the documentation at the link you provided and am unsure how any of that can help me. There is no other list than FilesOver30.files to compare it with.
If I understand your problem description correctly, the value of
"unc_path" is in the FIlesOver30.files list.
You can remove it by the difference of a list containing a single
item: the unc_path.
You are looking win_file with the values of FilesOver30.files but setting the path for each of those loops to unc_path. This will just run the same thing, deleting that UNC path for every loop iteration. What you want to do is set ‘path: “{{ item.path }}”’ so that path is set to each of the found paths that you got in the previous step.