从文件夹列表中删除所有超过两天的文件夹

问题描述 投票:0回答:1

我有三个文件夹。我需要 Ansible 来检查它们并删除超过两天的。

这些是我的任务:

- name: Get older files path
  find:
    paths: "{{ path }}/DB/{{ item }}/"
    age: 2d
    file_type: directory
    recurse: yes
  register: filesolderthan2
  with_items:
    - "{{ bucket }}"

- name: Remove older files
  file:
    path: item.path
    state: absent
  with_items: "{{ filesolderthan2.results }}"

可变文件

bucket:
  - test1
  - test2
  - test3
path: "/backup"

我确实得到了路径,但无法删除所有这些文件夹。
当我尝试使用

path: {{ item.path }}

听写错误

ansible ansible-2.x
1个回答
1
投票

find
模块将列出字典的
files
字段中匹配的所有文件。

由于您是在循环下注册的,因此在那些多个

results
字段之上有一个
files
字段。

所以,你可以做的是使用几个

map
flatten
过滤器来获得所有这些
files

- name: Remove older files
  file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ filesolderthan2.results | map(attribute='files') | flatten(1) }}"
© www.soinside.com 2019 - 2024. All rights reserved.