Proper syntax for ansible symlink

25,318

From the documentation:

src, path of the file to link to (applies only to state=link). Will accept absolute, relative and nonexisting paths. Relative paths are not expanded.

You need to use src and dest when using state=link, not path. You also need to invert your source and destination, which appear to be the wrong way around (assuming that you really want to link to sites-available from sites-enabled) — src is the path to link to, dest is where to create the symlink.

- name: Create symlink to example.com
  file: src=/etc/nginx/sites-available/example.com dest=/etc/nginx/sites-enabled/example.com state=link
  notify: nginx reload
Share:
25,318

Related videos on Youtube

jeanpier_re
Author by

jeanpier_re

Updated on September 18, 2022

Comments

  • jeanpier_re
    jeanpier_re over 1 year

    What is the proper syntax for creating symlinks using ansible? I'm creating a Vagrant virtual environment and running ansible as my provisioner. Everything works fine up until I try to symlink to my site nginx configuration file from sites-enabled. Here is what I've tried.

    - name: Create symlink to example.com
      file: 
        path: /etc/nginx/sites-enabled/example.com 
        dest: /etc/nginx/sites-available/example.com 
        state: link
      notify: nginx reload
    

    When I do this I'm met with the following error.

    ERROR: Syntax Error while loading YAML script, /Users/username/project/ansible/roles/nginx/tasks/main.yml

    Note: The error may actually appear before this position: line 24, column 1

    - name: Symlink to example.dev.conf

    file: path=/etc/nginx/sites-enabled/example.dev.conf

    state=link

    ^ Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

    I've also tried using src=/etc/nginx/sites-enabled instead of path but I can't seem to find a definitive example of this on the web.

    Specs:

    • OSX Mavericks
    • vagrant 1.6.3
    • VirtualBox 4.3
    • nginx 1.6.0
    • ansible 1.6.2
    • ubuntu 14.04