How to symlink in saltstack latest version or file

Viewed 248
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 ?

2 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.

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

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

works for me... thx for help

Related