下面的代码只删除它在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


- name: Delete content & directory
  file:
    state: absent
    path: /home/mydata/web/

注意:这也会删除目录。


使用壳模块(也是幂等的):

- shell: /bin/rm -rf /home/mydata/web/*

如果有点/隐藏文件:

- shell: /bin/rm -rf /home/mydata/web/* /home/mydata/web/.*

如果你不关心创建日期和所有者/权限,最干净的解决方案:

- file: path=/home/mydata/web state=absent
- file: path=/home/mydata/web state=directory

尝试下面的命令,它应该可以工作

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

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

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

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

删除目录(基本上是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
      }}

假设您一直在Linux中,尝试查找cmd。

- name: Clean everything inside {{ item }}
  shell: test -d {{ item }} && find {{ item }} -path '{{ item }}/*' -prune -exec rm -rf {} \;
  with_items: [/home/mydata/web]

这将清除隐藏在/home/mydata/web下的文件/文件夹


下面对我很有用,

- name: Ansible delete html directory
  file:
   path: /var/www/html
   state: directory

而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: "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

根据所有的评论和建议创建了一个全面的重新检查和故障安全实现:

# collect stats about the dir
- name: check directory exists
  stat:
    path: '{{ directory_path }}'
  register: dir_to_delete

# delete directory if condition is true
- name: purge {{directory_path}}
  file:
    state: absent
    path: '{{ directory_path  }}'
  when: dir_to_delete.stat.exists and dir_to_delete.stat.isdir

# create directory if deleted (or if it didn't exist at all)
- name: create directory again
  file:
    state: directory
    path: '{{ directory_path }}'
  when: dir_to_delete is defined or dir_to_delete.stat.exist == False

我真的不喜欢rm解决方案,也ansible给你关于使用rm的警告。 因此,这里是如何做到这一点,而不需要rm和ansible警告。

- hosts: all
  tasks:
  - name: Ansible delete file glob
    find:
      paths: /etc/Ansible
      patterns: "*.txt"
    register: files_to_delete

  - name: Ansible remove file glob
    file:
      path: "{{ item.path }}"
      state: absent
    with_items: "{{ files_to_delete.files }}"

来源:http://www.mydailytutorials.com/ansible-delete-multiple-files-directories-ansible/


  - name: delete old data and clean cache
    file:
      path: "{{ item[0] }}" 
      state: "{{ item[1] }}"
    with_nested:
      - [ "/data/server/{{ app_name }}/webapps/", "/data/server/{{ app_name }}/work/" ]
      - [ "absent", "directory" ]
    ignore_errors: yes

我希望确保find命令只删除目录内的所有内容,并保持目录完整,因为在我的示例中,目录是一个文件系统。当尝试删除文件系统时,系统将生成一个错误,但这不是一个好的选择。我使用壳选项,因为这是唯一的工作选项,我发现这个问题到目前为止。

我做了什么:

编辑hosts文件,放入一些变量:

[all:vars]
COGNOS_HOME=/tmp/cognos
find=/bin/find

并创建一个剧本:

- hosts: all
  tasks:
  - name: Ansible remove files
    shell: "{{ find }} {{ COGNOS_HOME }} -xdev -mindepth 1 -delete"

这将删除COGNOS_HOME变量目录/文件系统中的所有文件和目录。"-mindepth 1"选项确保不会触及当前目录。


我已经写了一个自定义ansible模块来清理基于多个过滤器的文件,如年龄,时间戳,glob模式等。

它也兼容一个可靠的旧版本。可以在这里找到。

这里有一个例子:

- cleanup_files:
  path_pattern: /tmp/*.log
  state: absent
  excludes:
    - foo*
    - bar*

如果你使用Ansible >= 2.3(文件和dirs之间的区别不再需要了),只是一个简单的复制和粘贴ThorSummoners的模板。

- name: Collect all fs items inside dir
  find:
    path: "{{ target_directory_path }}"
    hidden: true
    file_type: any
  changed_when: false
  register: collected_fsitems
- name: Remove all fs items inside dir
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ collected_fsitems.files }}"
  when: collected_fsitems.matched|int != 0

这就是我想到的:

- 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获取目录属性 删除目录 重新创建具有相同属性的目录。

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


是不是很简单…测试工作..

eg.

---
- hosts: localhost
  vars:
     cleandir: /var/lib/cloud/
  tasks:
   - shell: ls -a -I '.' -I '..' {{ cleandir }}
     register: ls2del
     ignore_errors: yes
   - name: Cleanup {{ cleandir }}
     file:
       path: "{{ cleandir }}{{ item }}"
       state: absent
     with_items: "{{ ls2del.stdout_lines }}"

下面的代码适合我:

- name: Get directory listing
  become: yes
  find:
    paths: /applications/cache
    patterns: '*'
    hidden: yes
  register: directory_content_result

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

下面是投票最多的答案(我无法编辑,因为“编辑队列已满”):

- name: Delete content & directory
  file:
    state: absent
    path: /home/mydata/web/

- name: Re-create the directory
  file:
    state: directory
    path: /home/mydata/web/

- name: Files to delete search
  find:
    paths: /home/mydata/web/
    file_type: any
  register: files_to_delete

- name: Deleting files to delete
  file:
    path: '{{ item.path }}'
    state: absent
  with_items: "{{ files_to_delete.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

因为它是:

简单的 幂等 快