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

查看:299
本文介绍了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天全站免登陆