问题描述
我试图弄清我在一个不完整的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变量应如何定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!