我正在通过盐抓取数据,我试图遍历所有类型的支柱。它运行但不会渲染任何东西。
{% for type in salt['pillar.get']('pillar') %}
{% if type in [Action', 'Queue', 'Data', 'Number'] %}
{% for something in salt['pillar.get']('pillar:{{ type }}:subtype') %}
copy_init_script_{{ type }}_{{ something }}:
file.managed:
- name: /blah/blah/somefile-{{ ptype }}-{{ something }}
- source: salt:/folder/anothertemplate.jinja
- template: jinja
- context:
something: {{ something }}
atype: {{ type }}
- timeout: 10
{% endfor %}
{% endif %}
{% endfor %}
为什么for循环似乎没有得到任何数据?
答案 0 :(得分:3)
您尝试在方法调用中使用Jinja变量表达式。相反,只需使用字符串连接直接使用变量,就像普通的Python一样。
{% for something in salt['pillar.get']('pillar:' + type + ':subtype') %}
请注意,它是+ type +
而不是{{ type }}
。
答案 1 :(得分:0)
salt['pillar.get']
功能需要密钥。默认情况下,没有pillar
密钥。只是支柱词典中键/值对的键。
如果您只想迭代支柱数据中的所有键/值对,只需直接遍历支柱词典
这样的事情:
{% for type in pillar %}
{% if type in [Action', 'Queue', 'Data', 'Number'] %}
{% for something in salt['pillar.get']('pillar:{{ type }}:subtype') %}
copy_init_script_{{ type }}_{{ something }}:
file.managed:
- name: /blah/blah/somefile-{{ ptype }}-{{ something }}
- source: salt:/folder/anothertemplate.jinja
- template: jinja
- context:
something: {{ something }}
atype: {{ type }}
- timeout: 10
{% endfor %}
{% endif %}
{% endfor %}
我还没有对这段代码进行过测试,但它应该接近这一点。