# Ensure a directory contains only specified files?

**URL:** <https://forum.ansible.com/t/ensure-a-directory-contains-only-specified-files/11903>\
**Category:** Ansible Project\
**Created:** [September 17, 2012, 3:34pm UTC](https://forum.ansible.com/t/ensure-a-directory-contains-only-specified-files/11903 "2012-09-17T15:34:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Kleint](https://avatars.discourse-cdn.com/v4/letter/j/df705f/32.png) [@John\_Kleint](https://forum.ansible.com/u/John_Kleint)\
**Post date:** [September 17, 2012, 3:34pm UTC](https://forum.ansible.com/t/ensure-a-directory-contains-only-specified-files/11903/1 "2012-09-17T15:34:13Z")

</div>

This is probably simple, but I’m not sure of the latest and greatest way to do it. In my playbook I have a var containing a list of filenames, and I copy them to a target directory on my nodes. I’d like to make sure those are the _only_ files in that dir. I’d really rather not delete everything and re-copy every time, as it’s kinda wasteful, the files might be in use, the timestamps become meaningless, and you lose the idempotent thing. Is there an Ansible way to do this, or do I bust out the bashhammer and/or rsync?

---

<div class="post-metadata">

**Author:** ![CWS](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@CWS](https://forum.ansible.com/u/CWS)\
**Post date:** [September 17, 2012, 3:36pm UTC](https://forum.ansible.com/t/ensure-a-directory-contains-only-specified-files/11903/2 "2012-09-17T15:36:16Z")

</div>

Sync to a staging directory, remove the old ones, and generate links  
to the files in the staging directory?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [September 17, 2012, 3:41pm UTC](https://forum.ansible.com/t/ensure-a-directory-contains-only-specified-files/11903/3 "2012-09-17T15:41:50Z")

</div>

I'd consider calling rsync --delete via local\_action/command.

(Note: I've suggested it seems to make sense to patch things to make  
local\_action assume "-c ssh" regardless of play settings)
