这个 Ansible 变量应该如何定义? [英] How should this Ansible variable be defined?
问题描述
我试图理解我在一个不完整的 Ansible 角色中发现的变量引用.角色使用
I am trying to make sense of a variable reference I found in an incomplete Ansible role. The role references a value using
dest: "{{params['box'].t1}}"
在一个单独的 yaml 文件中我有
In a separate yaml file I have
box:
t1: "Albany"
t2: "Albuquerque"
params 没有定义,所以显然这行不通,但我想不出定义它的正确方法.谁能告诉我必须在何处(或如何)定义参数才能在 Ansible 中使用此变量引用?
params isn't defined, so obviously this isn't going to work, but I can't figure out the correct way to define it. Can someone tell me where (or how) params must be defined for this variable reference to work in Ansible?
相关问题.dest:{{params['box'].t1}}"
中使用方括号是否表示它是字典?如果是,我是否也可以将其写为 dest: {{params['box']['t1']}"
或 dest: {{params.box.t1}"
?
Related questions. Does the use of square brackets in dest: "{{params['box'].t1}}"
indicate that it is a dictionary? If yes, could I also write this as dest: "{{params['box']['t1']}"
or dest: "{{params.box.t1}"
?
推荐答案
params['box'].t1
指的是 Albany
在:
params:
box:
t1: "Albany"
t2: "Albuquerque"
与params.box.t1
和params['box']['t1']
相同.
括号指的是一个键名,所以它们暗示它是一个字典.
Brackets refer to a key name, so they imply it is a dictionary.
当您想通过变量引用键时,通常使用方括号表示法:
You typically use square bracket-notation when you want to refer to a key via a variable:
vars:
wanted_key: box
params:
box:
t1: Albany
other:
t1: Albuquerque
那么params[wanted_key].t1
指的是Albany
.
在您的示例中,方括号内的值是一个字符串(带引号),因此上述所有示例都是等效的.
In your example the value inside square brackets is a string (quoted), so all above examples are equivalent.
这篇关于这个 Ansible 变量应该如何定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!