Making 100% sure content of directory will not get deleted

15 views
Skip to first unread message

Wasim Khamlichi

unread,
Nov 9, 2016, 2:10:36 PM11/9/16
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages