Ansible:如何删除目录中的文件和文件夹?

问题描述:

以下代码仅删除它在Web目录中获得的第一个文件.我想删除Web目录中的所有文件和文件夹,并保留Web目录.我怎样才能做到这一点?

The below code only deletes the first file it gets inside the web dir. I want to remove all the files and folders inside the web directory and retain the web directory. How can I do that?

  - name: remove web dir contents
     file: path='/home/mydata/web/{{ item }}' state=absent
     with_fileglob:
       - /home/mydata/web/*

注意:我已经使用命令和shell尝试了rm -rf,但是它们不起作用.也许我用错了它们.

Note: I've tried rm -rf using command and shell, but they don't work. Perhaps I am using them wrongly.

在正确方向上的任何帮助将不胜感激.

Any help in the right direction will be appreciated.

我正在使用ansible 2.1.0.0

I am using ansible 2.1.0.0

下面的代码将删除artifact_path

- name: Clean artifact path
  file:
    state: absent
    path: "{{ artifact_path }}/"

注意:这也会删除目录.