代码之家  ›  专栏  ›  技术社区  ›  DragonSGA

ansible-删除大量单个文件

  •  0
  • DragonSGA  · 技术社区  · 6 年前

    我想从不同的文件夹中删除垃圾文件。 (是的,几个模块的程序员都很马虎,没有正确的清理方法,但是没有发现…)。

    在我的易懂的剧本中,我有以下简单的任务来删除超过1小时但没有特定名称的文件(模式regex)。这就是为什么我不能简单地清除这两个文件夹。较年轻的文件需要保留,一些文件无法访问(由模式指定)。

    - 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需要时间来完成第三项任务。

    有没有办法更快地完成这项工作?我的意思是只需要调用一次文件模块。有什么想法吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   DragonSGA    6 年前

    - 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/