gpt4 book ai didi

python - 在ansible中合并字典

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:27 24 4
gpt4 key购买 nike

我目前正在构建一个使用 ansible 安装 PHP 的角色,并且在合并字典时遇到一些困难。我尝试了多种方法来做到这一点,但我无法让它像我想要的那样工作:

# A vars file:
my_default_values:
key = value

my_values:
my_key = my_value


# In a playbook, I create a task to attempt merging the
# two dictionaries (which doesn't work):

- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: my_default_values + my_values

# I have also tried:

- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: my_default_values|union(my_values)

# I have /some/ success with using j2's update,
# but you can't use j2 syntax in "with_dict", it appears.
# This works:

- debug: msg="{{ my_default_values.update(my_values) }}"

# But this doesn't:

- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: my_default_values.update(my_values)

有没有办法合并两个字典,这样我就可以将它与“with_dict”一起使用?

最佳答案

在 Ansible 2.0 中,有一个 Jinja 过滤器,combine ,为此:

- debug: msg="{{ item.key }} = {{ item.value }}"
with_dict: "{{ my_default_values | combine(my_values) }}"

关于python - 在ansible中合并字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59747632/

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