win_file removing root directory when deleting files older than 30 days

230 views
Skip to first unread message

Jeremy Smith

unread,
May 12, 2021, 12:48:45 PM5/12/21
to Ansible Project
Hello All, 

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. 

Thanks in advance for the feedback! 

Jeremy Smith

unread,
May 12, 2021, 12:49:47 PM5/12/21
to Ansible Project
For reference, the unc_path variable seen above is the path to the diagnostics folder. 

Dick Visser

unread,
May 12, 2021, 1:32:52 PM5/12/21
to ansible...@googlegroups.com
First thing that comes to mind is removing the folder from the
FilesOver30.files list, for instance using 'difference':
https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#selecting-from-sets-or-lists-set-theory
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/8a2f9dc4-5c99-459c-89a2-20bc488a4c0fn%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT

Jeremy Smith

unread,
May 12, 2021, 3:44:35 PM5/12/21
to Ansible Project
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. 

Dick Visser

unread,
May 12, 2021, 4:06:07 PM5/12/21
to ansible...@googlegroups.com
On Wed, 12 May 2021 at 21:44, Jeremy Smith <smith.j...@gmail.com> wrote:
>
> 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.

>> >> - name: Delete all files/folders older than 30 days from diagnostics folder
>> >> win_file:
>> >> path: "{{ unc_path }}"
>> >> state: absent
>> >> with_items: "{{ FilesOver30.files }}"

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.


with_items: "{{ FilesOver30.files | difference([unc_path]) }}"

jbor...@gmail.com

unread,
May 12, 2021, 5:00:02 PM5/12/21
to Ansible Project
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.
Message has been deleted
Message has been deleted

Jeremy Smith

unread,
May 12, 2021, 5:20:58 PM5/12/21
to Ansible Project
Thank you, that does seem to have done the trick. 
Reply all
Reply to author
Forward
0 new messages