如何在annsbile playbook中使用变量的第二个值?

问题描述 投票:0回答:1

我想在两个不同的地方使用一个组变量的2个值。我现在的组变量是这样的。

[test1:vars]
fooname=foo1
barname=bar1

我根据我的要求,在剧本中的多个地方使用jinja模板作为{{ fooname }}和{{ barname }}。现在,我不想用两个不同的变量,而是想用一个变量作为名字,并且想在不同的地方使用它的值。

预计组变量 。

[test1:vars]
names=foo1,bar1

有没有一种方法可以让我像在条件中使用的那样,在playbook中使用{{ names is search(foo) }}或{{ names is search(bar) }}这样的函数或条件来调用{{ names }}变量,从而避免声明两个变量而不是一个变量。我将在我的playbook中的不同地方使用这些变量。

我试过使用上面的变量,但它会打印 "True",而不是当我进行相应的搜索时,我需要我的变量的值,其中只有foo或bar。

注意:我有接近400组,同样的模式,使额外的变量使我的库存冗长。所以,我想尽量减少它。

ansible yaml ansible-2.x playbook
1个回答
0
投票

Ansible 2支持库存文件中的Arrays。

[test1:vars]
names=["foo","bar"]

但是有一些限制。

[example:vars]
# working
var1=["foo","bar"]
var2=[1,2]
var3=[True, False]

# not working
var4=[yes, no] # Boolean need to be True and False
var5=[foo, bar] # Interpreted as one string
var6='["foo","bar"]' # Interpreted as one string as well

使用示例。

- debug:
  - msg: "Item: {{ example[0] }}"

- debug:
  - msg: "Item: {{ example | first }}"
© www.soinside.com 2019 - 2024. All rights reserved.