Warm tip: This article is reproduced from stackoverflow.com, please click
ansible

Set Ansible fact based on symbolic link

发布于 2020-03-29 12:45:43

I have an Ansible task to install Anaconda:

---
- name: "Set variables"
  set_fact:
    package: "/software/anaconda/Anaconda3-2019.07-Linux-x86_64.sh"

- set_fact:
    anadir: "/opt/anaconda/anaconda3-2019.07"

- name: "Check existence"
  stat:
    path: "{{ anadir }}"
  register: anaconda

- debug:
    msg: "{{ anadir }} already exists."
  when: anaconda.stat.exists == True

- name: Install Anaconda
  shell: "/bin/bash {{ package }} -b -p {{ anadir }}"
  when: anaconda.stat.exists == False

But I don't want to have the version hardcoded in the file. I don't even want it in any kind of variable.

If I have a link in my directory /software/anaconda like latest -> Anaconda3-2019.07-Linux-x86_64.sh I am sure I can set a fact with the value Anaconda3-2019.07-Linux-x86_64.sh. But I could not find an elegant way to do it.

Questioner
lanes
Viewed
63
Zeitounator 2020-01-31 16:09

Run the stat module against your latest link, register the result and explore it.

You should find a lnk_source key pointing to your original file.

Ref: stat module return value specification