【问题标题】:AnsibleUndefinedVariable: No variable found with this name: region1a"}AnsibleUndefinedVariable:未找到具有此名称的变量:region1a"}
【发布时间】:2020-11-25 15:37:14
【问题描述】:

我想在比较区域是否为首都后得到信息的输出? 帮我弄清楚如何正确使用“查找”?

{% if capital == lookup('vars', item) %} yes {% else %} no {% endif %}

{% if capital.action == {{ item }} %} yes {% else %} no {% endif %}

我收到以下错误

failed: [localhost] (item=region1a) => {"ansible_loop_var": "item", "changed": false, "item": "region1a", "msg": AnsibleError: template error while templating string: expected token ':', got '}'

这里 {{ item }} 是一个变量 = region1a

我有这些变量

vars:
        AllCountry:
          - name1
          - name2
        name1:
          - region1a
          - region1b 
        name2:
          - region2a
          - region2b
        capital:
          - region1a
          - region2a

我错在哪里?

【问题讨论】:

    标签: ansible jinja2 ansible-template


    【解决方案1】:

    在我看来,这就是您想要实现的目标:

    {% if item in capital %} yes {% else %} no {% endif %}
    

    But that is really not foolproof。想象一下,你在两个国家有两个同名的地区,一个是其中一个国家的首都,而另一个不是另一个国家的首都,你会在这里怎么做?

    我真的会使用更紧密的字典,例如:

    countries:
      country1:
        regions:
          - region1
          - region2
          - region3
        capital: region1
      country2:
        regions:
          - region4
          - region5
        capital: region5
    

    但如果没有您的实际用例以及您尝试使用所有这些构建的内容,很难就构建正确类型的数据结构提出建议。

    【讨论】:

    • 我使用建议的结构并得到以下错误:AnsibleUndefinedVariable: 'masterN' is undefined.
    • 当然,如果你重新设计你的数据结构,这完全是一个不同的问题。
    • 它可以作为一个单独的问题吗?并与此联系。 stackoverflow.com/questions/64977059/…
    猜你喜欢
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多