小编San*_*s54的帖子

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

我有一些问题似乎无法克服。我有一个包含很多文件夹的文件夹。我想删除所有早于三天的文件夹,但是我至少要保留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 ... …
Run Code Online (Sandbox Code Playgroud)

ansible

7
推荐指数
1
解决办法
3224
查看次数

标签 统计

ansible ×1