Skip to content

Instantly share code, notes, and snippets.

@gMagicScott
Created November 25, 2015 18:04
Show Gist options
  • 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
@gMagicScott
Copy link
Author

Thanks

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