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

On Tue, 16 Jun 2020 at 9:05 PM, Mike Sawyers <magic...@gmail.com> wrote:

> 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
>
> ---
> - hosts: localhost
>   connection: local
>   tasks:
>     - name: create sub directories
>       file: path=/test/{{ item }} state=directory
>       with_items: ['a','b','c']
>
> returns:
>
> TASK [Gathering Facts]
> *********************************************************
> ok: [localhost]
>
> TASK [create sub directories]
> **************************************************
> changed: [localhost] => (item=a)
> ok: [localhost] => (item=b)
> changed: [localhost] => (item=c)
>
> PLAY RECAP
> *********************************************************************
> localhost                  : ok=2    changed=1    unreachable=0    failed=
> 0    skipped=0    rescued=0    ignored=0
>
>
> sh-4.4# ls -la /test
> total 16
> drwxr-xr-x 4 root root 4096 Jun 16 15:07 .
> drwxr-xr-x 1 root root 4096 Jun 16 15:01 ..
> drwxr-xr-x 2 root root 4096 Jun 16 15:07 a
> lrwxrwxrwx 1 root root    8 Jun 16 15:06 b -> /usr/tmp
> drwxr-xr-x 2 root root 4096 Jun 16 15:07 c
>
> This seems like it just works but maybe my basic play doesn't included
> everything or the test setup is missing something.
>
> To loop the the reported problem of getting an error when a target dir
> already exists as a link to something, I'll try and cover the methods that
> I know of along with some comments on each.
>
> - You could go with a blanket assertion that the job never fails:
>
> ---
> - hosts: localhost
>   connection: local
>   tasks:
>     - name: create sub directories
>       file: path=/test/{{ item }} state=directory
>       with_items: ['a','b','c']
>       failed_when: false
>
> This will 'work' but will also mask any other failures that come up for
> any other reason. I only ever use that in very specific cases where I am
> sure beyond doubt that it is fine to never fail.
>
> - You could be selective about failing, but honestly this method is hard
> for me to do cleanly on the fly and always takes me a few attempts to get
> every condition just so. A non-working example would be something like:
>
> ---
> - hosts: localhost
>   connection: local
>   tasks:
>     - name: create sub directories
>       file: path=/test/{{ item }} state=directory
>       with_items: ['a','b','c']
>       register: mk_subdir
>       failed_when: (mk_subdir.state is not defined) or (mk_subdir.state is
> defined and mk_subdir.state != 'link')
>
> I didn't run this example since I was unable to duplicate the issue you
> reported having, but it is a pattern of using selective failed_when to
> control failure. This is also in the docs on error handeling.
>
> - Lastly and easiest for me to keep in my headspace you could be selective
> about your list by checking for symlinks first
>
> ---
> - hosts: localhost
>   connection: local
>   vars:
>     subdir_list: [ 'a','b','c' ]
>   tasks:
>     - name: check sub directories
>       stat:
>         path: "/test/{{ item }}"
>         get_attributes: no
>         get_checksum: no
>         get_md5: no
>         get_mime: no
>       register: chk_subdirs
>       loop: "{{ subdir_list }}"
>
>     - set_fact:
>         mkdirs: "{{ subdir_list | difference(item) }}"
>       loop: "{{ chk_subdirs.results | selectattr('stat.islnk') |
> map(attribute='item') | list }}"
>
>     - name: create sub directories
>       file:
>         path: "/test/{{ item }}"
>         state: directory
>       loop: "{{ mkdirs }}"
>
> This first checks each target directory you want to create with stat, then
> any item from that check that is a link is removed from the list of dirs
> that will be created. Lastly I create any directory left in the list. The
> result from this version is
> PLAY [localhost]
> *************************************************************************************************************************************************************************************************
>
> TASK [Gathering Facts]
> *******************************************************************************************************************************************************************************************
> ok: [localhost]
>
> TASK [check sub directories]
> *************************************************************************************************************************************************************************************
> ok: [localhost] => (item=a)
> ok: [localhost] => (item=b)
> ok: [localhost] => (item=c)
>
> TASK [set_fact]
> **************************************************************************************************************************************************************************************************
> ok: [localhost] => (item=b)
>
> TASK [create sub directories]
> ************************************************************************************************************************************************************************************
> ok: [localhost] => (item=a)
> ok: [localhost] => (item=c)
>
> PLAY RECAP
> *******************************************************************************************************************************************************************************************************
> localhost                  : ok=4    changed=0    unreachable=0    failed=
> 0    skipped=0    rescued=0    ignored=0
>
> So we see that `b` was removed from the list of created sub dirst because
> we know it is a symlink.
>
>
>
>
>
> On Friday, June 12, 2020 at 10:26:03 AM UTC-7, Akansha wrote:
>>
>> 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.
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-devel+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-devel/2dab2455-dd74-4be7-8895-17156f09b484o%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-devel/2dab2455-dd74-4be7-8895-17156f09b484o%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
-- 
Sent from Gmail Mobile

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-devel/CAENxsS%2BkdT%2Bjq85YxN2qnPmujnGhfpnKLKOjWTFYadmCQeRk0g%40mail.gmail.com.

Reply via email to