Ansible:删除文件夹内的所有文件和目录,而不是文件夹本身

Ansible : delete all files and directories inside a folder and not the folder itself

我正在尝试 运行 删除项目文件夹中所有内容的任务,而不删除文件夹本身:

我的文件夹 : appFolder ----包含----> file1 , file2 ... fileN , directory1 .. .目录N

我试过使用 lsfileGlob ,但效果不佳。

- name: delete old version files
  file:
   path: "{{ paths }}"
   state: absent
  with_filesglob:
   - "{{paths}}deletes/*"

还有 shell 模块(不好,因为它没有删除具有特殊字符名称的文件)

- shell: ls -1 /some/dir
  register: contents

- file: path=/some/dir/{{ item }} state=absent
  with_items: {{ contents.stdout_lines }}

更好的解决方案?

在修复 #18910 之前(state=empty for dirs),我们一直在寻找解决方法。

这是其中之一:

- name: Clean build folder
  shell: cd /project/build && find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf --