Cannot create symbolic link in Ansible, says: "folder is not empty, refusing to convert it"

13,632

To create a symlink "/home/ubuntu/productName" to your folder "/var/www/html/simpleRepoTwo/simpleRepoThree/release-{{release_folder}}", you must set "/home/ubuntu/productName" in your parameter dest. Final '/' must not be used

About force parameter, use it in two cases:

  • the source file does not exist (but will appear later)
  • destination exists and is a file (so, we need to unlink the "path" file and create symlink to the "src" file in place of it).

Use this task

- name: Creating symlinks of Products
  file:
    src: /var/www/html/simpleRepoTwo/simpleRepoThree/release-{{release_folder}}
    dest: /home/ubuntu/productName
    state: link

To loop over items, use this task

- name: Creating symlinks of Products
  file:
    src: "/var/www/html/simpleRepoTwo/simpleRepoThree/release-{{ release_folder }}/{{ item.src }}"
    dest: "/home/ubuntu/{{ item.dest }}"
    state: link
  with_items:
  - src: p1
    dest: p1
  - src: p2
    dest: p2
Share:
13,632
Shirshak
Author by

Shirshak

Updated on June 04, 2022

Comments

  • Shirshak
    Shirshak almost 2 years

    Error:

    FAILED! => {"changed": false, "failed": true, "gid": 0, "group": "root", "mode": "0755", "msg": "the directory /var/www/html/simpleRepoTwo/simpleRepoThree/release-20161213-1927/ is not empty, refusing to convert it", "owner": "root", "path": "/var/www/html/simpleRepoTwo/simpleRepoThree/release-20161213-1927/", "size": 4096, "state": "directory", "uid": 0}

    i've also tried creating symlink in /home/ubuntu/ with different directories. force:yes did not work.

    Here is the code

    - name: Creating symlinks of Products
      file:
        src: /home/ubuntu/productName/
        dest: /var/www/html/simpleRepoTwo/simpleRepoThree/release-{{release_folder}}/
        state: link
        force: yes
    

    I want to create symlink of productName inside simpleRepoTwo/simpleRepoThree/release-20161213-1927

  • Shirshak
    Shirshak over 7 years
    Hello @Nelson G, symlink was created, but then again i tried to loop it, since we have around 8 products, so when user types ourwebsite.com/admin it will land in adminpanel.
  • Shirshak
    Shirshak over 7 years
    - name: Creating symlinks of Products file: src: '/var/www/html/{{ item.src }}/' dest: '/var/www/html/simpleRepoTwo/simpleRepoThree/{{release_folde‌​r}}/{{ item.dest }}' state: link with_items: - { src: 'simpleRepo', dest: 'prodcutOne' } - { src: 'simpleRepo', dest: 'prodcutTwo' } - { src: 'simpleRepo', dest: 'prodcutThree' } - { src: 'simpleRepo', dest: 'prodcutFour' }
  • Shirshak
    Shirshak over 7 years
    Hello Nelson and tecraf, i tired to create symlinks using loops, both the syntax did not work. Here's gist. gist.github.com/shirshakparakhi/… Error(pretty print):: failed:[ 54.85 .157.222 ](item={ u'dest':u'prodcutSix', u'src':u'simpleRepo' }) =>{ "failed":true, "item":{ "dest":"prodcutSix", "src":"simpleRepo" }, "msg":"Error while linking: [Errno 2] No such file or directory", "path":"/var/www/html/simpleRepoTwo/simpleRepoThree/20161214‌​-1334/prodcutSix", "state":"absent" }
  • Shirshak
    Shirshak over 7 years
    If i run playbook in intervals, 8 product symlinks are not created evenly, sometimes it throws error, sometimes it just creates symlinks perfectly. But most of the time its just error: Error:: (item={u'dest': u'prodcutTwo', u'src': u'simpleRepo'}) => {"failed": true, "item": {"dest": "prodcutTwo", "src": "simpleRepo"}, "msg": "Error while linking: [Errno 2] No such file or directory", "path": "/var/www/html/simpleRepoTwo/simpleRepoThree/20161214-1540/p‌​rodcutTwo", "state": "absent"}