71

In my playbook, I need to create a symbolic link for a repo.

With command (shell) it may work like this:

########## Create symbolic link 
- name: Create symbolic link 
  shell : ln   -s  "{{SOURCE_FOLDER}}"  SYMLINK
  args :
    chdir : "/opt/application/i99/"
  when:
    - ansible_host in groups['ihm']

-> like this my symbolic link is created directly inside i99 repo /

SYMLINK -> SOURCE_FOLDER

But while doing it with the Ansible file module, like this:

########## Create symbolic link 
- name: Create symbolic link 
  file:
   src: "/opt/application/i99/{{SOURCE_FOLDER}}/"
   dest: "/opt/application/i99/SYMLINK"
   state: link
  when:
    - ansible_host in groups['ihm']

My output is this :

SYMLINK -> /opt/application/i99/SOURCE_FOLDER

As I don't want that it points to the whole path, and I need to obtain the first format:

SYMLINK -> SOURCE_FOLDER

How can I do it?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148

1 Answers1

127

Simply:

- name: Create symbolic link 
  file:
    src: "{{SOURCE_FOLDER}}"
    dest: "/opt/application/i99/SYMLINK"
    state: link

As you can see in the manual for the file module:

src  Will accept absolute, relative and nonexisting paths. Relative paths are not expanded.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
techraf
  • 64,883
  • 27
  • 193
  • 198
  • 7
    `dest:` is now being called `path:` in the documentation, but is still valid as an alias. – Serge Stroobandt Sep 23 '18 at 13:47
  • 2
    `dest` and `name` are valid aliases for `path` according to the latest documentation: https://docs.ansible.com/ansible/latest/modules/file_module.html – mPrinC Oct 15 '18 at 15:35