AnsibleUndefinedVariable:找不到具有此名称的变量:region1a“}

AnsibleUndefinedVariable: No variable found with this name: region1a"}

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

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

我哪里错了?

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

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

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