Search code examples
pythonjinja2salt-stack

Declaring Jinja variable in Salt state returns error: Jinja syntax error: expected token ',', got 'java'


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 :)


Solution

  • 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 '\"' ") %}