Skip to content

Instantly share code, notes, and snippets.

@gMagicScott
Created November 25, 2015 18:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gMagicScott/618d7ac29b12664d7482 to your computer and use it in GitHub Desktop.
Save gMagicScott/618d7ac29b12664d7482 to your computer and use it in GitHub Desktop.
Ansible - remove unmanaged files from a directory

from this question on Stack Overflow

Set up a managed_files variable as an array

- hosts: whatever
  vars:
    managed_files:
     - file1
     - file2
     - file3

In a playbook, use it like this:

- shell: ls -1 /some/dir
  register: contents

- file: path=/some/dir/{{ item }} state=absent
  with_items: contents.stdout_lines
  when: item not in managed_files
@dreua
Copy link

dreua commented May 19, 2022

See my answer on StackOverflow for an improved version of this using find instead of shell.

@gMagicScott
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment