gpt4 book ai didi

ansible - 根据 ansible 中的条件将项目添加到列表中

转载 作者:行者123 更新时间:2023-12-02 16:44:43 24 4
gpt4 key购买 nike

我想根据满足的某些条件将项目添加到 ansible 列表中。

这不起作用:

  some_dictionary:
app:
- something
- something else
- something conditional # only want this item when some_condition == True
when: some_condition

我不确定执行此操作的正确方法。我可以创建一个新任务以某种方式添加到 some_dictionary 中的 app 值吗?

最佳答案

您可以使用 select() 过滤掉所有错误值,但请记住应用 list()之后过滤。对我来说,这似乎是一种更简单、更易读的方法:

- name: Test
hosts: localhost
gather_facts: no
vars:
mylist:
- "{{ (true) | ternary('a','') }}"
- "{{ (false) | ternary('b','') }}"
- "{{ (true) | ternary('c','') }}"

tasks:
- debug:
var: mylist|select|list

结果:

TASK [debug] *****************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"mylist|select()|list": [
"a",
"c"
]
}

(true)(false) 替换为您想要的任何测试。

关于ansible - 根据 ansible 中的条件将项目添加到列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39165463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com