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

huangapple go评论133阅读模式
英文:

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

问题

我正在尝试在我的Salt状态中声明一个变量,该变量获取一个Shell命令('cmd.run')的输出,以便我可以在状态的其他地方使用它。它看起来像这样:

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

这个单独的Shell命令在我的服务器上运行时是正确的。但由于某种原因,在State中它返回了错误:

Jinja语法错误:预期的令牌',',得到了'java';第10行

(第10行是上面的变量声明)。我怎么也找不出出了什么问题...我是否应该在某个地方转义字符或做些其他的事情?我对Salt有点不熟悉,所以如果我漏掉了一些明显的东西,还请原谅!感谢任何帮助 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 Declaring Jinja variable in Salt state returns error: Jinja syntax error: expected token ',', got 'java'

答案1

得分: 1

问题在于嵌入在命令行中的 ' 字符与用于界定命令行本身的引号匹配。

请在命令行周围使用不同类型的引号。由于命令行内部同时使用单引号和双引号,最简单的解决方法是用三重引号包围它。

此外,它应该是一个原始字符串,以便 \. 中的反斜杠将被保留为字面值。

{% 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 '"' ''') %}
英文:

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

答案2

得分: 1

只返回翻译好的部分:

在你的命令中,问题出在混合使用了 ' 作为命令字符串的引号和作为 awk 过滤器内部的引号。

尝试:

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

我已经测试了 Python 版本,对我有效:

{% set minorVersion = salt['cmd.run']("/usr/bin/python -V 2>&1 | awk -F'python version' '{print $1}' | awk -F'\.' '{print $2; exit}'| tr -d '\"' ") %}
英文:

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

huangapple
  • 本文由 发表于 2023年8月11日 04:13:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76879036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定