# idempotent way to convert directory into a link?

**URL:** <https://forum.ansible.com/t/idempotent-way-to-convert-directory-into-a-link/23752>\
**Category:** Ansible Project\
**Created:** [February 17, 2017, 12:51am UTC](https://forum.ansible.com/t/idempotent-way-to-convert-directory-into-a-link/23752 "2017-02-17T00:51:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunnz](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sunnz](https://forum.ansible.com/u/sunnz)\
**Post date:** [February 17, 2017, 12:51am UTC](https://forum.ansible.com/t/idempotent-way-to-convert-directory-into-a-link/23752/1 "2017-02-17T00:51:57Z")

</div>

hi,

can ansible force a path to be a link even when it is a directory with files? currently i am deleting the directory before making it a link and it is not idempotent.

on the first run it deletes the directory. but it is also deleting and re-creating the link in every run after that.

for example of what i am trying to do:

- name: remove existing link or directory  
file:  
path: /var/lib/mysql  
state: absent
- name: link mysql to datastore  
file:  
src: /mnt/datastore/mysql  
dest: /var/lib/mysql  
state: link  
force: true

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 17, 2017, 7:38am UTC](https://forum.ansible.com/t/idempotent-way-to-convert-directory-into-a-link/23752/2 "2017-02-17T07:38:41Z")

</div>

> can ansible force a path to be a link even when it is a directory with  
> files? currently i am deleting the directory before making it a link and it  
> is not idempotent.

You can't force it, you need to delete and create link as you do now.

> on the first run it deletes the directory. but it is also deleting and  
> re-creating the link in every run after that.

Before you delete and create link use the stat module to check if the path is a directory and then do a delete and create the link.

[https://docs.ansible.com/ansible/stat\_module.html](https://docs.ansible.com/ansible/stat_module.html)

---

<div class="post-metadata">

**Author:** ![sunnz](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@sunnz](https://forum.ansible.com/u/sunnz)\
**Post date:** [March 3, 2017, 5:24am UTC](https://forum.ansible.com/t/idempotent-way-to-convert-directory-into-a-link/23752/3 "2017-03-03T05:24:53Z")

</div>

thanks kai, i got the stat to work in this way:
