1

I am trying to apply a salt.states.alternatives to set my default java to java 11. However, I need to put in the full path for the openjdk, which includes the version number that changes every time there is a version change (meaning I have to update salt state frequently):

set-java-11:
  alternatives.set:
    - name: java
    - path: /usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java

If I do alternatives --config java, I can see that the path is tied to a command and a selection number.

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.372.b07-1.el7_9.x86_64/jre/bin/java)
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.19.0.7-1.el7_9.x86_64/bin/java)

So my question is: Is there a way in which I can set the salt state to use selection 2 without referencing the full path (such as referencing the selection number or the command)

1 Answers1

0

The selection number is not predictable, so that is not going to work.

What you can do is adjust the salt state so you don't have to manually change the path every time:

{% if salt["pkg.version"]("java-11-openjdk") %}
set-java-11:
  alternatives.set:
    - name: java
    - path: /usr/lib/jvm/{{ salt["cmd.run"]("rpm -q java-11-openjdk") }}/bin/java
{% endif %}

Unfortunately a slot is not directly usable as there's no way to prepend text to the result. However, you could write a simple custom module to do it that can then be used in a slot:

def java_path():
  return "/usr/lib/jvm/" + __salt__["cmd.run"]("rpm -q java-11-openjdk") + "/bin/java"
set-java-11:
  alternatives.set:
    - name: java
    - path: __slot__:salt:myutils.java_path()
    - require:
      - pkg: java-11-openjdk
OrangeDog
  • 569
  • 4
  • 20