Create a directory when symlink already exists

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.

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

`

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