I can, of course, use a shell command, but I was hoping there was an ansible way to do this, so that I can get the "changed/unchanged" response.
Asked
Active
Viewed 1.3k times
13
-
2Hmmmm it does appear as if http://stackoverflow.com/questions/16385507/ansible-delete-unmanaged-files-from-directory might work. – Christian Goetze Nov 27 '13 at 20:13
2 Answers
16
Here's how I do it:
- name: Capture files to delete
find:
paths: /path/to/directory
file_type: file
excludes:
- "myfirst.file"
- "mysecond.file"
register: found_files
- name: Delete files
file:
path: "{{ item.path }}"
state: absent
with_items: "{{ found_files['files'] }}"

Lars Nyström
- 5,786
- 3
- 32
- 33
2
In addition to what you found per your comment, if you end up needing to do a shell command, you can use the command
module with removes
parameter (doc). It will skip the step if the file already (doesn't) exist, which will prevent it from reporting a changed
on the step. You'll still need to iterate over a list like in the other answer, however.

Community
- 1
- 1

Jake Kreider
- 950
- 7
- 12