【问题标题】:rendering SLS 'base:test.test1' failed: Jinja variable 'list object' has no attribute 'sub1'渲染 SLS 'base:test.test1' 失败:Jinja 变量 'list object' 没有属性 'sub1'
【发布时间】:2021-06-25 05:11:38
【问题描述】:

我的 SLS 文件如下,当尝试访问变量 tst1 时出现错误,详细信息如下

{% load_yaml as test %}
    value:
      val1: 'string1'
      val2: 'string2'
    value1: ['sub1','sub2']
{% endload %}

{%- for tst in test.value1 -%}

{% set tst1 = test.value1[tst] %}

{{ tst1 }}

{%- endfor -%}

当尝试访问变量 tst1 时,我得到如下错误。

rendering SLS 'base:test.test1' failed: Jinja variable 'list object' has no attribute 'sub1'

谁能帮忙看看到底是什么错误?

【问题讨论】:

  • 一旦你通过迭代得到tst1变量,你想用它做什么?

标签: salt-stack


【解决方案1】:

{% set tst1 = test.value1[tst] %}

在这一行中,您试图访问一个字典。 test.value1 是一个列表,而不是一个字典。 我认为这正是您想要的:

代码

{%- for tst in test.value1 -%}

{{ tst }}

{%- endfor -%}

输出

sub1sub2

【讨论】:

  • 感谢@SynPrime。我在上面尝试过,但没有成功...因为 ``` rendering SLS 'base:test.test1' failed: 找不到预期的 ':' ```
【解决方案2】:

实际上,SLS 文件应该包含要与模块一起执行的 ID(任务)。喜欢:

do-something:
  module.name:
    - args
    ...

仅仅放置像{{ tst }} 这样的东西是行不通的,因为它期望像do-something: 这样的映射

所以在for 循环中,你应该“调用”一些模块。我使用test.echo 来显示以下示例中的值:

{% for tst in test.value1 %}
show-value1-{{ tst }}:
  module.run:
    - name: test.echo
    - text: '{{ tst }}'
{% endfor %}

【讨论】:

    猜你喜欢
    • 2018-11-18
    • 2019-07-23
    • 1970-01-01
    • 2020-10-14
    • 2023-03-14
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多