Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to symlink in saltstack latest version or file

Tags:

salt-project

ln -fs /opt/app/$(ls -rt file-*.jar | tail -n1) /opt/app/file.jar

works in bash very well

dir contains

file-1.jar
file-2.jar
file-3.jar

How can I do this in a salt stack state sls formula ?

like image 326
LongLastingNewbie Avatar asked Feb 02 '26 02:02

LongLastingNewbie


1 Answers

To achieve this in Saltstack we need roughly two steps:

  1. Get the latest file
  2. Link the file

For the first part, we can use some salt module such as file.find, but I feel the existing logic of using ls -rt is simpler.

So we can use this command to get the latest JAR file into a variable. Then use a Salt state to link the file.

Example:

{% set latest_jar = salt.cmd.run('ls -rt /opt/app/file-*.jar | tail -n1') %}

link-latest-jar:
  file.symlink:
    - name: /opt/app/file.jar
    - target: {{ latest_jar }}

Update:

With newer version of Saltstack we need to use salt.cmd.shell to set the latest_jar variable.

like image 58
seshadri_c Avatar answered Feb 03 '26 17:02

seshadri_c