gpt4 book ai didi

ansible - 双重条件-删除所有3天以上的文件夹,但至少保留10天

转载 作者:行者123 更新时间:2023-12-04 11:19:22 25 4
gpt4 key购买 nike

我有一些问题似乎无法克服。我有一个包含很多文件夹的文件夹。我想删除所有早于三天的文件夹,但是我至少要保留10个文件夹。

我提出了这个半工作的代码,我想提出一些建议来解决这个问题。

---
- hosts: all
tasks:
# find all files that are older than three
- find:
paths: "/Users/asteen/Downloads/sites/"
age: "3d"
file_type: directory
register: dirsOlderThan3d

# find all files that are in the directory
- find:
paths: "/Users/asteen/Downloads/sites/"
file_type: directory
register: allDirs

# delete all files that are older than three days, but keep a minimum of 10 files
- file:
path: "{{ item.path }}"
state: absent
with_items: "{{ dirsOlderThan3d.files }}"
when: allDirs.files > 10 and not item[0].exists ... item[9].exists

最佳答案

您只需要过滤3天以上的文件列表:

---
- hosts: all
tasks:
- name: find all files that are older than three
find:
paths: "/Users/asteen/Downloads/sites/"
age: "3d"
file_type: directory
register: dirsOlderThan3d
- name: remove older than 3 days but first ten newest
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ (dirsOlderThan3d.files | sort(attribute='ctime'))[:-10] | list }}"

关于ansible - 双重条件-删除所有3天以上的文件夹,但至少保留10天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com