AnsibleError:模板化字符串时出现模板错误:预期标记“:”,得到“}”

问题描述 投票:0回答:1

准备模板时出现错误。谁能告诉你怎么解决?

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

  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"
ansible templating ansible-template
1个回答
11
投票

发生这种情况是因为您在 Jinja 的语句分隔符

{{
中嵌套了表达式分隔符
{%

{% for count in {{ country }} %}
{#              ^--- right there #}

为了实现您想要做的事情,您可以使用

vars
查找。

给定任务:

- 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

这产生:

ok: [localhost] => 
  msg: |2-
     {name: "name1",My country = region1a My country = region1b  {name: "name2",My country = region2a My country = region2b  
© www.soinside.com 2019 - 2024. All rights reserved.