gpt4 book ai didi

jinja2 - Ansible & Jinja2 : combine a dictionary to each element of a list

转载 作者:行者123 更新时间:2023-12-01 12:24:53 27 4
gpt4 key购买 nike

我有一个数组变量 [{'foo':1},{'bar':2}] .
我想将它与以下哈希结合起来:{'baz':3}使用设置事实(?),例如我的输出注册变量是:

[{'foo':1, 'baz':3},{'bar':2, 'baz':3}]

我查看了 combine过滤器,但它只在我已经有一个哈希值时才有效。就我而言,我有一个数组。

有没有办法使用ansible来实现这一目标?

最佳答案

其实我已经找到方法了。 map可以与任何过滤器一起使用,参数必须在逗号后传递

- name: test
set_fact:
_test: "{{ [{'foo':1}, {'bar':2}] | map('combine', {'baz':3}) | list }}"

产生:
ok: [localhost] => {
"_test": [
{
"baz": 3,
"foo": 1
},
{
"bar": 2,
"baz": 3
}
]
}

关于jinja2 - Ansible & Jinja2 : combine a dictionary to each element of a list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40900787/

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