gpt4 book ai didi

templating - AnsibleError : template error while templating string: expected token ':' , 得到 '}'

转载 作者:行者123 更新时间:2023-12-04 15:12:25 28 4
gpt4 key购买 nike

准备模板时发生错误。谁能告诉你怎么修?
如有必要,还可以编辑变量。

  vars:
AllСountry:
- "name1"
- "name2"
name1:
- "region1a"
- "region1b"
name2:
- "region2a"
- "region2b"
代码
{% for country in AllСountry %}   
{name: "{{ country }}",{% for count in {{ country }} %}My country = {{ count }}
{% endfor %}{% endfor %}
结果是错误
AnsibleError:模板字符串时模板错误:预期标记“:”,得到“}”
是的,最后我希望得到整个列表的输出
name: "name1  My country = "region1a" My country = "region1b"   
name: "name2: My country = "region2a" My country = "region2b"

最佳答案

发生这种情况是因为您正在嵌套表达式分隔符 {{在语句分隔符中 {%在金贾这里:

{% for count in {{ country }} %}
{# ^--- right there #}
为了实现您的目标,您可以使用 vars 抬头。
鉴于剧本:
- hosts: all
gather_facts: no

tasks:
- debug:
msg: >
{% for country in AllCountry %}
{name: "{{ country }}",{% for count in lookup('vars', country) %}My country = {{ count }}
{% endfor %}{% endfor %}
vars:
AllCountry:
- name1
- name2
name1:
- region1a
- region1b
name2:
- region2a
- region2b
这产生了回顾:
PLAY [all] *******************************************************************************************************

TASK [debug] *****************************************************************************************************
ok: [localhost] => {
"msg": " {name: \"name1\",My country = region1a My country = region1b {name: \"name2\",My country = region2a My country = region2b \n"
}

PLAY RECAP *******************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

请注意 lookup('vars', country)也可以缩短为 vars[country]

关于templating - AnsibleError : template error while templating string: expected token ':' , 得到 '}',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64977059/

28 4 0