AnsibleUndefinedVariable:找不到具有此名称的变量:region1a"} [英] AnsibleUndefinedVariable: No variable found with this name: region1a"}

查看:29
本文介绍了AnsibleUndefinedVariable:找不到具有此名称的变量:region1a"}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to get the output of information after comparing whether the region is the capital? Help me figure out how to use "lookup" correctly?

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

here {{ item }} is a variable = region1a

我有这些变量

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

我哪里错了?

推荐答案

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

It seems to me like this is what you are looking to achieve:

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

但这真的不是万无一失.假设您有两个国家/地区名称相同的两个地区,一个是其中一个国家/地区的首都,而另一个地区不是另一个国家/地区的首都,您会在这里怎么做?

But that is really not foolproof. Imagine you have two regions named the same in two countries and one is the capital of one of the countries when the other is not the capital of the other one, how would you do it here?

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

I would really go with a more tied kind of dictionary like:

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

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

But without your actual use case and what you are trying to build with all this, this is hard to advise on the right type of data structure to construct.

这篇关于AnsibleUndefinedVariable:找不到具有此名称的变量:region1a"}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆