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

问题描述 投票:119回答:15

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

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

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

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

我使用的是ansible 2.1.0.0

ansible delete-file delete-directory
15个回答
122
投票

下面的代码将删除artifact_path的全部内容

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

注意:这也会删除目录。


1
投票

我想确保find命令只删除目录中的所有内容并保持目录完整,因为在我的情况下,目录是一个文件系统。尝试删除文件系统时系统会生成错误,但这不是一个好的选择。我使用shell选项,因为这是我到目前为止找到的唯一一个工作选项。

我做了什么:

编辑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变量目录/ filesystem中的所有文件和目录。 “-mindepth 1”选项确保不会触及当前目录。


0
投票

假设您总是在Linux中,请尝试使用find 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下的文件/文件夹/隐藏


0
投票
先删除对应的目录,然后在创建该目录,使用的是嵌套循环
  - 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

0
投票

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

它还与ansible旧版本兼容。它可以找到here

这是一个例子:

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

0
投票

如果你使用的是Ansible> = 2.3,那么只需要一个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

0
投票

跟随下面:

- hosts: all
  tasks:
    - name: Empty remote directory
      file:
        path: /home/felipe/files
        state: absent
      become: yes

    - name: Create
      file:
        path: /home/felipe/files
        state: directory
        owner: jboss
        group: jboss
        mode: u=rwx,g=rx,o=rx
      become: yes

-4
投票

以下对我有用,

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

59
投票

使用shell模块(幂等):

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

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

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

41
投票

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

17
投票

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

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

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

15
投票

我真的不喜欢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/


5
投票

使用文件glob也可以。您发布的代码中存在一些语法错误。我已经修改并测试了这应该工作。

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

4
投票

从所有意见和建议中创建了一个全面的修复和故障安全实施:

# 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

2
投票

虽然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 }}"

但请注意,通过同步,您应该能够正确地同步文件(删除)。


2
投票

关于这一点,有一个issue open

目前,该解决方案适用于我:在本地创建一个空文件夹并将其与远程文件夹同步。

这是一个示例剧本:

- 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
© www.soinside.com 2019 - 2024. All rights reserved.