Score:1

Saltstack set java alternative using command instead of path

mx flag

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)

Score:0
il flag

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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.