Reputation: 11
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
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. I want to delete the test folders from all the subdirectories.
Upvotes: 1
Views: 45
Reputation: 68034
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 }}"
Upvotes: 1