Symbolic link creation failed when provisioning

I am trying to create a VM as similar as possible to my server using Vagrant and Ansible for provisioning.

Every Ansible role works fine except the symlink creation and I am not able to fix it. I work in a Windows OS and my server is Linux.

My links.yml task configuration is:

---
- name: Create directories
  file:
    path: '{{ ansible_env.HOME }}/{{ item.folder }}'
    state: directory
  with_items: '{{ server.links }}'
  when: server.links is defined


# IT REACHES THIS POINT WITHOUT ERRORS, CREATE DIRECTORIES TASK SEEMS TO WORK FINE


- name: Create symbolic links
  file:
    src: '{{ ansible_env.HOME }}/{{ item.folder }}'
    dest: '{{ item.dest }}/{{ item.folder }}'
    state: link
  with_items: '{{ server.links }}'
  when: server.links is defined

And the error shown when provisioning reaches the task:

failed: [testserver-vm] (item={u'dest': u'/vagrant', u'folder': u'vendor'}) => {"changed": false, "gid": 1000, "group"
: "vagrant", "item": {"dest": "/vagrant", "folder": "vendor"}, "mode": "0777", "msg": "refusing to convert between direc
tory and link for /vagrant/vendor", "owner": "vagrant", "path": "/vagrant/vendor", "size": 0, "state": "directory", "uid
": 1000}
failed: [testserver-vm] (item={u'dest': u'/vagrant', u'folder': u'node_modules'}) => {"changed": false, "gid": 1000, "
group": "vagrant", "item": {"dest": "/vagrant", "folder": "node_modules"}, "mode": "0777", "msg": "refusing to convert b
etween directory and link for /vagrant/node_modules", "owner": "vagrant", "path": "/vagrant/node_modules", "size": 0, "s
tate": "directory", "uid": 1000}

I am not really familiar with this kind of error and I don't know how to proceed.

Edit: I've also tried to force the symbolic link creation using force: true as a parameter, in this case, the error message is:

failed: [testserver-vm] (item={u'dest': u'/vagrant', u'folder': u'vendor'}) => {"changed": false, "gid": 1000, "group"
: "vagrant", "item": {"dest": "/vagrant", "folder": "vendor"}, "mode": "0777", "msg": "the directory /vagrant/vendor is
not empty, refusing to convert it", "owner": "vagrant", "path": "/vagrant/vendor", "size": 4096, "state": "directory", "
uid": 1000}

If it helps, here is my Vagrantfile:

Vagrant.require_version '>= 1.8.5'

Vagrant.configure('2') do |config|
    config.hostmanager.enabled = true
    config.hostmanager.manage_host = true
    config.hostmanager.manage_guest = true

    config.vm.provider :virtualbox do |v|
        v.customize [
            'modifyvm', :id,
            '--name', 'testserver-vm',
            '--cpus', 1,
            '--memory', 2048,
            '--natdnshostresolver1', 'on',
            '--nictype1', 'virtio',
            '--nictype2', 'virtio',
        ]
    end

    config.vm.define 'testserver-vm' do |node|
        node.vm.box = 'ubuntu/xenial64'
        node.vm.network :private_network, ip: '192.168.33.4', nic_type: 'virtio'
        node.vm.network :forwarded_port, host: 5000, guest: 5000, auto_correct: true
        node.vm.network :forwarded_port, host: 5001, guest: 5001, auto_correct: true
        node.vm.hostname = 'testserver.local'
        node.vm.synced_folder './', '/vagrant', type: 'virtualbox'
        node.ssh.forward_agent = true
    end

    config.vm.provision 'ansible_local' do |ansible|
        ansible.playbook = 'ansible/playbook.yml'
        ansible.install_mode = :pip
        ansible.version = "2.5.2"
    end

    config.vm.provision "file", source: "~/.ssh/id_rsa", destination: "/home/ubuntu/.ssh/id_rsa"
    config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "/home/ubuntu/.ssh/id_rsa.pub"
end

Many thanks!

1 answer

  • answered 2018-05-16 06:25 Ada Pongaya

    Looks like the destination ( {{ item.dest }}/{{ item.folder }}) you are specifying already exists as a directory.
    Delete the destination(directory) and execute the same.

    sample code:

    ---
    - name : Test Sym Link
      hosts: localhost
      tasks:
        - name : Create a Link
          file :
            dest : /home/ec2-user/my_link
            src : /home/ec2-user/my_src_directory
            state: link
    

    PS: Don't Create destination as a directory for symlinks