【发布时间】:2020-12-14 02:35:45
【问题描述】:
我正在 ansible 中创建一个简单的字典,就是这个:
"types_dict": {
"name1": "Ethernet",
"name2": "Ethernet",
"name3": "Software-Pseudo",
"name4": "Ethernet",
"name5": "Software-Pseudo",
"name6": "Ethernet" }
我的目标是循环字典并用“虚拟”替换一些特定值,即“软件伪”。我尝试了以下方法:
- set_fact:
types_dict: "{{ types_dict | combine(new_item, recursive=true) }}"
vars:
new_item: "{ '{{ item.key }}': { 'type': 'Virtual' } }"
with_dict: "{{ types_dict }}"
但这里的问题是这个更新了我字典中的所有值,这是我根本不想要的。我还通过添加“when”语句尝试了以下操作,但它也不起作用:
- set_fact:
types_dict: "{{ types_dict | combine(new_item, recursive=true) }}"
vars:
new_item: "{ '{{ item.key }}': { 'type': 'Virtual' } }"
when: "{{ item.value }} == Software-Pseudo"
with_dict: "{{ types_dict }}"
我还尝试了when: "{{ item.value }} == 'Software-Pseudo'" 和许多其他类似的东西。
关于如何解决这个问题的任何想法?
【问题讨论】:
标签: loops dictionary ansible