【问题标题】:Saltstack - Unable to print output in Jinja templatingSaltstack - 无法在 Jinja 模板中打印输出
【发布时间】:2021-12-05 05:24:19
【问题描述】:

我试图在 Saltstack 中重用命令的输出,但是当我尝试使用“cmd.run”打印输出时,它失败并出现以下错误。不确定从“cmd.run”返回的数据格式。

{% set output = salt['cmd.shell']('ifconfig') %}

display:
  cmd.run:
    - name: echo '{{ output }}'

错误:

    Data failed to compile:
----------
    Rendering SLS 'base:patching.install_patches' failed: mapping values are not allowed in this context

【问题讨论】:

    标签: jinja2 salt-stack


    【解决方案1】:

    问题似乎是由于引号的位置。整个命令都需要单引号才能运行。

    以下应该可以:

    {% set output = salt['cmd.shell']('ifconfig') %}
    
    display:
      cmd.run:
        - name: 'echo "{{ output }}"'
    

    【讨论】:

    • 因为输出可能包含:,YAML 解析器试图解释
    • 很有可能,ifconfig 输出通常也非常冗长,包含多行。
    • {{ output | yaml_encode }} 将是更好的解决方案,以防它也包含引号
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2018-08-25
    相关资源
    最近更新 更多