【发布时间】:2014-10-14 21:02:20
【问题描述】:
我目前正在构建一个使用 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”一起使用?
【问题讨论】:
标签: python dictionary merge ansible