[英]Delete specific folder/files in all the directories of the server from the Ansible Playbook
I want to remove the all "test" folders inside the subdirectories of server.我想删除服务器子目录中的所有“测试”文件夹。 Like below is the path of "test" folders.如下所示是“测试”文件夹的路径。 There are multiple directories in home folder, So, I cant specify all the paths in playbook.主文件夹中有多个目录,因此,我无法在剧本中指定所有路径。
Path: /home/*/test
路径: /home/*/test
I have write the below playbook for this but it doesnt work.我为此编写了以下剧本,但它不起作用。
tasks:
- name: Delete the folder
file:
path: "{{ item }}"
state: absent
with_items:
- "/home/*/test"
Could you please let me know the solution for it..你能告诉我解决方案吗..
I have tried to use file_glob but doesn't work.我试过使用 file_glob 但没有用。 I want to delete the test folders from all the subdirectories.我想从所有子目录中删除测试文件夹。
Use the module find .使用模块查找。 For example, given the tree例如,给定树
shell> tree /tmp/home/
/tmp/home/
├── a
├── b
│ └── test
└── c
└── test
Declare the list of the paths声明路径列表
test_dirs: "{{ out.files|map(attribute='path') }}"
Then, the task然后,任务
- find:
paths: /tmp/home
file_type: directory
patterns: test
recurse: true
register: out
gives给
test_dirs:
- /tmp/home/c/test
- /tmp/home/b/test
Use the list to delete the directories使用列表删除目录
- file:
path: "{{ item }}"
state: absent
loop: "{{ test_dirs }}"
shell> tree /tmp/home/
/tmp/home/
├── a
├── b
└── c
Example of a complete playbook for testing用于测试的完整剧本示例
- 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.