Ansible - 删除大量的单个文件
Ansible - Delete large amount of individual files
我想从不同的文件夹中删除垃圾文件。
(是的,几个模块的程序员草率,没有正确清理,但没关系......)。
在我的 Ansible 剧本中,我有以下简单的任务来删除早于 1 小时且没有特定名称(模式正则表达式)的文件。这就是为什么我不能简单地清除这两个文件夹。较新的文件需要保留,有些文件是不可触及的(由模式指定)。
- 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 }}"
所以:我收集删除文件有两个事实。
然后我使用 Ansible 文件模块删除它们。
问题:有数千个文件和文件夹。获取要删除的内容列表只需要几秒钟。但是 Ansible 然后需要 ages 来完成第三个任务。
有没有办法更快地完成这项工作?我的意思是只调用一次文件模块。有什么想法吗?
用 Ansibles 命令模块重写(如果有人遇到这个并寻找类似的东西):
- 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/
我想从不同的文件夹中删除垃圾文件。 (是的,几个模块的程序员草率,没有正确清理,但没关系......)。
在我的 Ansible 剧本中,我有以下简单的任务来删除早于 1 小时且没有特定名称(模式正则表达式)的文件。这就是为什么我不能简单地清除这两个文件夹。较新的文件需要保留,有些文件是不可触及的(由模式指定)。
- 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 }}"
所以:我收集删除文件有两个事实。 然后我使用 Ansible 文件模块删除它们。
问题:有数千个文件和文件夹。获取要删除的内容列表只需要几秒钟。但是 Ansible 然后需要 ages 来完成第三个任务。
有没有办法更快地完成这项工作?我的意思是只调用一次文件模块。有什么想法吗?
用 Ansibles 命令模块重写(如果有人遇到这个并寻找类似的东西):
- 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/