下面的代码只删除它在web目录中的第一个文件。我想删除web目录中的所有文件和文件夹,并保留web目录。我该怎么做呢?

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

注意:我已经尝试了rm -rf使用命令和shell,但他们不工作。也许我用错了。

任何正确方向上的帮助都将不胜感激。

我使用ansible 2.1.0.0


当前回答

删除目录(基本上是https://stackoverflow.com/a/38201611/1695680的副本),Ansible在底层使用rmtree执行此操作。

- name: remove files and directories
  file:
    state: "{{ item }}"
    path: "/srv/deleteme/"
    owner: 1000  # set your owner, group, and mode accordingly
    group: 1000
    mode: '0777'
  with_items:
    - absent
    - directory

如果您无法删除整个目录并重新创建它,您可以扫描其中的文件(和目录),然后逐个删除它们。这需要一段时间。你可能想要确保在你的ansible.cfg on中有[ssh_connection]\npipelining = True。

- block:
  - name: 'collect files'
    find:
      paths: "/srv/deleteme/"
      hidden: True
      recurse: True
      # file_type: any  # Added in ansible 2.3
    register: collected_files

  - name: 'collect directories'
    find:
      paths: "/srv/deleteme/"
      hidden: True
      recurse: True
      file_type: directory
    register: collected_directories

  - name: remove collected files and directories
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: >
      {{
        collected_files.files
        + collected_directories.files
      }}

其他回答

我喜欢下面的解决方案:

- name: remove web dir contents
  command:
    cmd: "find . -path '*/*' -delete -print"
    chdir: "/home/mydata/web/"
  register: web_files_list
  changed_when: web_files_list.stdout | length > 0

因为它是:

简单的 幂等 快

而Ansible仍在讨论实现state = empty https://github.com/ansible/ansible-modules-core/issues/902

my_folder: "/home/mydata/web/"
empty_path: "/tmp/empty"


- name: "Create empty folder for wiping."
  file:
    path: "{{ empty_path }}" 
    state: directory

- name: "Wipe clean {{ my_folder }} with empty folder hack."
  synchronize:
    mode: push

    #note the backslash here
    src: "{{ empty_path }}/" 

    dest: "{{ nl_code_path }}"
    recursive: yes
    delete: yes
  delegate_to: "{{ inventory_hostname }}"

不过请注意,无论如何,使用synchronize你应该能够正确地同步你的文件(使用delete)。

这就是我想到的:

- name: Get directory listing
  find:
    path: "{{ directory }}" 
    file_type: any
    hidden: yes
  register: directory_content_result

- name: Remove directory content
  file:
    path: "{{ item.path }}" 
    state: absent
  with_items: "{{ directory_content_result.files }}" 
  loop_control:
    label: "{{ item.path }}" 

首先,我们用查找,设置获取目录列表

File_type转换为any,这样我们就不会错过嵌套目录和链接 隐藏为yes,所以我们不会跳过隐藏文件 另外,不要将递归设置为yes,因为这不仅没有必要,而且可能会增加执行时间。

然后,我们用文件模块遍历这个列表。它的输出有点啰嗦,因此输入loop_control。标签将帮助我们限制输出(在这里找到这个建议)。


但我发现以前的解决方案有点慢,因为它迭代的内容,所以我去:

- name: Get directory stats
  stat:
    path: "{{ directory }}"
  register: directory_stat

- name: Delete directory
  file:
    path: "{{ directory }}"
    state: absent

- name: Create directory
  file:
    path: "{{ directory }}"
    state: directory
    owner: "{{ directory_stat.stat.pw_name }}"
    group: "{{ directory_stat.stat.gr_name }}"
    mode: "{{ directory_stat.stat.mode }}"

使用stat获取目录属性 删除目录 重新创建具有相同属性的目录。

这对我来说已经足够了,但是如果你愿意,你也可以添加属性。

这方面还有一个悬而未决的问题。

目前,解决方案对我来说是可行的:在本地创建一个空文件夹,并将其与远程文件夹同步。

以下是一个范例:

- name: "Empty directory"
  hosts: *
  tasks:
    - name: "Create an empty directory (locally)"
      local_action:
        module: file
        state: directory
        path: "/tmp/empty"

    - name: Empty remote directory
      synchronize:
        src: /tmp/empty/
        dest: /home/mydata/web/
        delete: yes
        recursive: yes

使用文件glob也可以。你发布的代码有语法错误。我已经修改和测试这应该工作。

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