将项目添加到依赖于ansible中的条件的列表 [英] Add an item to a list dependent on a conditional in ansible
本文介绍了将项目添加到依赖于ansible中的条件的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望根据是否满足某些条件,将商品添加到列表中.
I would like to add an item to a list in ansible dependent on some condition being met.
这不起作用:
some_dictionary:
app:
- something
- something else
- something conditional # only want this item when some_condition == True
when: some_condition
我不确定执行此操作的正确方法.我可以创建新任务以某种方式添加到some_dictionary
中的app
值吗?
I am not sure of the correct way to do this. Can I create a new task to add to the app
value in the some_dictionary
somehow?
推荐答案
我会尽量避免这种情况,但是如果绝对需要条件列表,则可以使用以下技巧:
I'd try to avoid this, but if conditional list is absolutely necessary, you can use this trick:
---
- hosts: localhost
gather_facts: no
vars:
a: 1
b: 1
c: 2
some_dictionary:
app: "{{ '[\"something\", \"something else\"' + (a + b == c) | ternary(', \"something conditional\"',' ') + ']' }}"
tasks:
- debug: var=some_dictionary.app
它将形成一个类似数组的字符串(["item1","item2","item3"]
),并且在分配给app
之前,ansible变量模板将其转换为列表.
It will form an array-like string (["item1","item2","item3"]
) and ansible variable templator will convert it into list before assigning to app
.
这篇关于将项目添加到依赖于ansible中的条件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文