我有以下数据结构:
site_subnets:
control: { network: "100.99.97.0/24", mtu: "1500", vm_start_offset: 0, dhcp_from_ip: "30", dhcp_to_ip: "50" }
storage: { network: "100.99.98.0/24", mtu: "9000", vm_start_offset: 0, dhcp_from_ip: "30", dhcp_to_ip: "50" }
mission: { network: "100.99.99.0/24", mtu: "9000", vm_start_offset: 0, dhcp_from_ip: "30", dhcp_to_ip: "50" }
site: { network: "100.99.0.0/16", mtu: "1500", dhcp_from_ip: "30", dhcp_to_ip: "50" }
local: { network: "100.99.44.1/22", mtu: "1500", dhcp_from_ip: "30", dhcp_to_ip: "50" }
我想最终得到一个像这样的新字典:
site_subnets_simplified:
control: "100.99.97.0/24"
storage: "100.99.98.0/24"
mission: "100.99.99.0/24"
site: "100.99.0.0/16"
local: "100.99.44.1/22"
这是我写的剧本:
- set_fact:
site_subnets_flat: "{{ site_subnets | combine | dict2items }}"
- set_fact:
site_subnets_simplified: "{{ site_subnets_simplified|default({}) + [ { item: item.network } ] }}"
loop:
- "{{ site_subnets_flat }}"
当我运行它时,我得到一个错误,说'不可哈希类型:'list'">
是否有更好或更简单的方法来做到这一点?
例如
site_subnets_keys: "{{ site_subnets.keys()|list }}"
site_subnets_vals: "{{ site_subnets|json_query('*.network') }}"
site_subnets_simplified: "{{ dict(site_subnets_keys|zip(site_subnets_vals)) }}"
为
site_subnets_simplified:
control: 100.99.97.0/24
local: 100.99.44.1/22
mission: 100.99.99.0/24
site: 100.99.0.0/16
storage: 100.99.98.0/24