-1

I'm trying to declare a variable in my salt state that's getting the output of a shell command ('cmd.run') so I can use it somewhere else in the state. This is what it looks like:

{% set minorVersion = salt['cmd.run']('/path/to/directory/java/bin/./java -version 2>&1 | awk -F'java version' '{print $1}' | awk -F'\.' '{print $2; exit}'| tr -d '"'') %}

That shell command alone works correctly when running it on my server. But for some reason in the State it's returning that error,

Jinja syntax error: expected token ',', got 'java'; line 10

(line 10 is that above variable declaration). Can't for the life of me figure out what's going on.. should I be escaping a character somewhere or something? I'm a little inexperienced with Salt so sorry if I'm missing something obvious! Thanks for any help :)

2 Answers2

1

The problem is that the embedded ' characters inside the command line match the quotes that were used to delimit the command line itself.

Use different quotes around the command line. Since the command line uses both single and double quotes inside it, the simplest solution is to put triple-quotes around it.

Also, it should be a raw string so that the backslash in \. will be preserved literally.

{% set minorVersion = salt['cmd.run'](r'''/path/to/directory/java/bin/./java -version 2>&1 | awk -F'java version' '{print $1}' | awk -F'\.' '{print $2; exit}'| tr -d '"' ''') %}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I figured something was going on with escaping hah, thank you! although I added those changes and I'm now getting "Jinja syntax error: expected token ',', got 'string';" - any ideas? I don't know where it's getting string from, unless it means the raw string itself? – TorusWithSprinkles Aug 10 '23 at 20:44
  • 1
    Sorry, I don't know Jinja, so I'm not sure what that means. I assumed everything this just wanted Python syntax. – Barmar Aug 10 '23 at 20:51
1

It's because in your command you are mixing the usage of ' on the command string with usage of ' inside the command for awk filters

Try:

{% set minorVersion = salt['cmd.run']("/path/to/directory/java/bin/./java -version 2>&1 | awk -F'java version' '{print $1}' | awk -F'\.' '{print $2; exit}'| tr -d '\"' ") %}

I have tested the python version and it works for me

{% set minorVersion = salt['cmd.run']("/usr/bin/python -V 2>&1 | awk -F'python version' '{print $1}' | awk -F'\.' '{print $2; exit}'| tr -d '\"' ") %}
aekis.dev
  • 2,626
  • 1
  • 12
  • 19
  • this is definitely on the right track but for some reason I'm now getting "Jinja syntax error: expected token ',', got 'string';" - I got this for both triple single quotes and triple double quotes like you suggested, any ideas? Not sure what the heck is going on hah – TorusWithSprinkles Aug 11 '23 at 12:25
  • I have updated my answer after some tests in saltstack, now it should work for you – aekis.dev Aug 11 '23 at 17:00