How to create a relative symlink in Ansible?

AnsibleAnsible 2.x

Ansible Problem Overview


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?

Ansible Solutions


Solution 1 - Ansible

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfirasKoubaaView Question on Stackoverflow
Solution 1 - AnsibletechrafView Answer on Stackoverflow