Here is what I came up with:
- 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 }}"
First, we get a list of directories using find with the setting
file_type - any , so that we don’t miss nested directories and linkshidden - yes , so we do not skip hidden files- also do not set
recurse to yes , as this is not only unnecessary, but may increase the execution time.
Then we look at this list using the file module. The output is a bit verbose, so loop_control.label will help us with limiting the output (our tip here ).
But I found that the previous solution was somewhat slow as it iterates over the contents, so I decided:
- 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 }}"
- get directory properties using
stat - delete directory
- recreate a directory with the same properties.
That was enough for me, but you can also add attributes if you want.
Alexander Sep 22 '19 at 17:18 2019-09-22 17:18
source share