# Create a directory when symlink already exists

**URL:** <https://forum.ansible.com/t/create-a-directory-when-symlink-already-exists/33166>\
**Category:** Ansible Developer\
**Created:** [June 12, 2020, 5:26pm UTC](https://forum.ansible.com/t/create-a-directory-when-symlink-already-exists/33166 "2020-06-12T17:26:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akansha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Akansha](https://forum.ansible.com/u/Akansha)\
**Post date:** [June 12, 2020, 5:26pm UTC](https://forum.ansible.com/t/create-a-directory-when-symlink-already-exists/33166/1 "2020-06-12T17:26:02Z")

</div>

Hi,

I am new to Ansible and I have written a task to create sub directories at a given path using item/loop. The code is working in normal conditions, but it fails if soft/symbolic link already exists for any of the items.

- name: create sub directories

file: path=/etc/{{ item }} state=directory  
with\_items: “{{ subdir\_list }}”

I tried to add various WHEN conditions to avoid creating the sub directories, but nothing worked.

**error message** :  
“msg”: “///users/aks/etc/test already exists as a link”

Below is one sample WHEN condition :

**when: item.state is not defined or (item.state is defined and item.state != “link”)**

Please advise how to resolve this issue.

---

<div class="post-metadata">

**Author:** ![Mike\_Sawyers](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@Mike\_Sawyers](https://forum.ansible.com/u/Mike_Sawyers)\
**Post date:** [June 16, 2020, 3:35pm UTC](https://forum.ansible.com/t/create-a-directory-when-symlink-already-exists/33166/2 "2020-06-16T15:35:30Z")

</div>

Didn’t realize I didn’t reply all when I wrote back to Akansha, so to start, here is the additional clearification:

> Just to clarify, I want to skip error that occurs because of existing “link”. But error should be thrown if it is caused because of any other reason.

With that said, there are a few ways that I have done this in the past ranging from bad to good. I’ll try and cover what I know just to give context to the new users to the best I know how to explain it.

My test area in a CentOS 8 docker and ansible 2.9.9:

`  
sh-4.4# ls -la /test  
total 8  
drwxr-xr-x 2 root root 4096 Jun 16 15:01 .  
drwxr-xr-x 1 root root 4096 Jun 16 15:01 …  
lrwxrwxrwx 1 root root 17 Jun 16 15:01 b → /usr/tmp

`

and a playbook of

`

---

<div class="post-metadata">

**Author:** ![Akansha](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Akansha](https://forum.ansible.com/u/Akansha)\
**Post date:** [June 17, 2020, 5:30pm UTC](https://forum.ansible.com/t/create-a-directory-when-symlink-already-exists/33166/3 "2020-06-17T17:30:34Z")

</div>

Thanks Mike! After discussing the requirement further, I decided to delete existing symbolic link and proceed. So issue is resolved now.
