[英]Ansible; using a loop to cycle through variables
I have an inventory in which many host_vars are set.我有一个清单,其中设置了许多 host_vars。 Each host will contain a different number of datasets.
每个主机将包含不同数量的数据集。
eg例如
host: host1
ip-addr: 192.0.2.12/24
datasets:
set1:
var1: 'east'
var2: 'west'
set2:
var1: 'north'
var2: 'south'
I can create a loop to count the datasets, but I don't seem able to use it to reference [varX]:我可以创建一个循环来计算数据集,但我似乎无法使用它来引用 [varX]:
- name: "test loop"
debug:
msg:
- "{{ item }}"
- "{{ 'datasets.set' + item + '.var1' }}"
- "{{ datasets.set1.var1 }}"
loop: "{{ query('sequence', 'start=1 end='+((datasets|length)|string)) }}"
This appears to assemble the variable name I'm attempting to reference, however does not return the value associated with it.这似乎组合了我试图引用的变量名称,但不返回与之关联的值。 Manually calling that variable does return the interesting value.
手动调用该变量确实会返回有趣的值。
ok: [host1] => (item=1) => {
"msg": [
"1",
"datasets.set1.var1",
"east"
]
}
ok: [host1] => (item=2) => {
"msg": [
"2",
"datasets.set2.var1",
"east"
]
}
Is what I'm doing possible, or should I be approaching it from another angle?我正在做的事情是可能的,还是应该从另一个角度接近它?
thanks in advance.提前致谢。
The task任务
- debug:
msg: "set{{ item }}.var1 = {{ datasets['set' ~ item].var1 }}"
loop: "{{ range(1, datasets|length+1)|list }}"
gives给
"msg": "set1.var1 = east"
"msg": "set2.var1 = north"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.