Skip to content

Instantly share code, notes, and snippets.

@brandonkal
Created July 28, 2018 22:49
Show Gist options
  • Save brandonkal/3e33b8f871db24b60e1dc65a7be9f784 to your computer and use it in GitHub Desktop.
Save brandonkal/3e33b8f871db24b60e1dc65a7be9f784 to your computer and use it in GitHub Desktop.
ansible-rm-files-not-whitelisted
---
- name: Delete files from directory not in whitelist
hosts: localhost
gather_facts: false
vars:
whitelist:
- a
- b
- c
tasks:
- name: Get list of items in dir
command: ls -1
args:
chdir: ./files_rm
register: the_files
- name: delete files not in whitelist list
file:
path: ./files_rm/{{ item }}
state: absent
when: item not in whitelist
loop: "{{ the_files.stdout_lines }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment