簡體   English   中英

從 Ansible Playbook 中刪除服務器所有目錄中的特定文件夾/文件

[英]Delete specific folder/files in all the directories of the server from the Ansible Playbook

我想刪除服務器子目錄中的所有“測試”文件夾。 如下所示是“測試”文件夾的路徑。 主文件夾中有多個目錄,因此,我無法在劇本中指定所有路徑。

路徑: /home/*/test

我為此編寫了以下劇本,但它不起作用。

tasks:
   - name: Delete the folder
     file:
       path: "{{ item }}"
       state: absent
     with_items:
       - "/home/*/test"

你能告訴我解決方案嗎..

我試過使用 file_glob 但沒有用。 我想從所有子目錄中刪除測試文件夾。

使用模塊查找 例如,給定樹

shell> tree /tmp/home/
/tmp/home/
├── a
├── b
│   └── test
└── c
    └── test

聲明路徑列表

  test_dirs: "{{ out.files|map(attribute='path') }}"

然后,任務

    - find:
        paths: /tmp/home
        file_type: directory
        patterns: test
        recurse: true
      register: out

  test_dirs:
  - /tmp/home/c/test
  - /tmp/home/b/test

使用列表刪除目錄

    - file:
        path: "{{ item }}"
        state: absent
      loop: "{{ test_dirs }}"
shell> tree /tmp/home/
/tmp/home/
├── a
├── b
└── c

用於測試的完整劇本示例

- hosts: localhost vars: test_dirs: "{{ out.files|map(attribute='path') }}" tasks: - find: paths: /tmp/home file_type: directory patterns: test recurse: true register: out - debug: var: test_dirs - file: path: "{{ item }}" state: absent loop: "{{ test_dirs }}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM