Ansible错误:模板字符串时出现模板错误:预期的令牌":",'}'



准备模板时发生错误。谁能告诉你怎么修?

如有必要,还可以编辑变量。

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"

发生这种情况是因为您在Jinja中的语句分隔符{%中嵌套了表达式分隔符{{

{% 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   

相关内容

  • 没有找到相关文章

最新更新