Reputation: 340
I want to remove the garbage files from different folders. (Yes, the programmers of several modules were sloppy and did not cleanup the right way, but nevermind...).
In my Ansible playbook I have the following simple tasks to delete files older than 1h and not having a specific name (pattern regex). This is why i cannot simply clear the two folder. Younger files need to stay and some files are untouchable (specified by pattern).
- name: cleanup | find temporary files for removal
find:
paths: /var/xx/yy/temporary/
recurse: no
file_type: file
age: 1h
age_stamp: mtime
patterns:
- '^.*(?<!index\.html)$'
use_regex: yes
register: xxyy_temporary_files
- name: cleanup | find public/temporary files for removal
find:
paths: /var/zzz/public/temporary/
recurse: yes
file_type: any
age: 1h
age_stamp: mtime
patterns:
- '^.*(?<!index\.html)$'
use_regex: yes
register: zzz_public_temporary_files
- name: cleanup | remove garbage files
file:
path: "{{ item.path }}"
state: absent
with_items:
- "{{ xxyy_temporary_files.files }}"
- "{{ zzz_public_temporary_files.files }}"
loop_control:
label: "{{ item.path }}"
So: I collect the files for deletion in two facts. Then I use the Ansible file module to remove them.
The problem: there are thousands of files and folders. Getting the lists of what to delete only needs seconds. But Ansible then needs ages for the third task.
Is there a way to get this done quicker? I mean like calling the file module only once for all. Any ideas?
Upvotes: 1
Views: 2934
Reputation: 340
Rewritten with Ansibles command module (if anybody comes across this and looks for something like that):
- name: cleanup | remove old temporary files
command: "find {{ item }} -type f -mmin +30 -not -name \"index.html\" -not -name \".htaccess\" -print -delete -maxdepth 1"
register: xxx_old_tmp_files
changed_when: xxx_old_tmp_files.stdout != ""
with_items:
- /var/xxx/temporary/
- name: cleanup | remove old public/temporary files
command: "find {{ item }} -type f -mmin +30 -not -name \"index.html\" -print -delete"
register: yyy_old_pubtmp_files
changed_when: yyy_old_pubtmp_files.stdout != ""
with_items:
- /var/yyy/public/temporary/
- name: cleanup | remove old empty public/temporary folders
command: "find {{ item }} -type d -mmin +30 -empty -print -delete"
register: zzz_old_pubtmp_empty_folders
changed_when: zzz_old_pubtmp_empty_folders.stdout != ""
with_items:
- /var/zzz/public/temporary/
Upvotes: 2