在ansible中合并字典

我目前正在使用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) }}"

以上是 在ansible中合并字典 的全部内容, 来源链接: utcz.com/qa/405881.html

回到顶部