# Making 100% sure content of directory will not get deleted

**URL:** <https://forum.ansible.com/t/making-100-sure-content-of-directory-will-not-get-deleted/22988>\
**Category:** Ansible Project\
**Created:** [November 9, 2016, 1:33pm UTC](https://forum.ansible.com/t/making-100-sure-content-of-directory-will-not-get-deleted/22988 "2016-11-09T13:33:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wasim\_Khamlichi](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@Wasim\_Khamlichi](https://forum.ansible.com/u/Wasim_Khamlichi)\
**Post date:** [November 9, 2016, 1:33pm UTC](https://forum.ansible.com/t/making-100-sure-content-of-directory-will-not-get-deleted/22988/1 "2016-11-09T13:33:27Z")

</div>

I’m writing a play to create a few symlinks in my app directory that link to some shared directories on a NFS mount.

Because of the way the application is, in the location where I’m trying to place the symlink, there may or may not be a directory that already exists. I want to make sure that directory is deleted first, then a symlink created in it’s place to point to the corresponding directory on the NFS mount.

I have a play which does this fine but I wanted some input to make sure 100% that if the symlink is already created, then deleting the directory with NEVER delete any contents from the NFS mount.

`

# set list of directories as variable

- name: Set dirs variable  
set\_fact:  
dirs\_to\_symlink:
- { src: /nfs/audio, dest: /var/web/site/audio}
- { src: /nfs/downloads, dest: /var/web/site/downloads}
- { src: /nfs/images, dest: /var/web/site/images}

# get stat information about the directories

- name: check if directories exist  
stat: path={{ item.dest }}  
register: dir\_check  
with\_items: “{{ dirs\_to\_symlink }}”

- debug: msg=“exists={{ item.stat.exists }}, islnk={{ item.stat.islnk }}, isdir={{ item.stat.isdir }}”  
with\_items: “{{ dir\_check.results }}”

# go through the stats and do some checks, if directories exist delete them, otherwise leave it alone

- name: remove directories ready for sym links  
file: path={{ item.item.dest }} state=absent  
when:
- item.stat.exists == true
- item.stat.islnk == false
- item.stat.isdir == true  
with\_items: “{{ dir\_check.results }}”

# go ahead and create symlinks to directories on the nfs mount

- name: create sym links for assets  
file: src={{ item.src }} dest={{ item.dest }} state=link force=yes  
with\_items: “{{ dirs\_to\_symlink }}”  
`

This works fine, it doesn’t delete the contents of the directory on the NFS mount if the symlink does exist. Great!

But if checking the following enough?

`

- item.stat.exists == true
- item.stat.islnk == false
- item.stat.isdir == true  
`

Are there any other things I can check for to make sure that…

`file: path={{ item.item.dest }} state=absent`

Will not accidentally on purpose delete a directory on the NFS mount…which would be very very bad!

Any advise or extra checks that come to mind would be awesome.
