我从Ansible
中提取了以下数据neighbour_details:
Port1:
- host: SW1
platform: Switch
Port2:
- host: RTR2
platform: Router
Port3:
- host: SW3
platform: Switch
trunk_ports:
- Port1
- Port3
我正在寻找一种方法来过滤基于trunk_port列表上的任何邻居详细信息,以获得以下输出。
neighbour_filtered:
Port1:
- host: SW1
platform: Switch
Port3:
- host: SW3
platform: Switch
我尝试了各种方法,但还是无法理解我们在Ansible中是如何做到的。
例如
- set_fact:
neighbour_filtered: "{{ dict(trunk_ports|zip(values)) }}"
vars:
values: "{{ trunk_ports|map('extract', neighbour_details)|flatten }}"
为
neighbour_filtered:
Port1:
host: SW1
platform: Switch
Port3:
host: SW3
platform: Switch
如果您想保留列表,请删除过滤器,使变平,例如
- set_fact:
neighbour_filtered: "{{ dict(trunk_ports|zip(values)) }}"
vars:
values: "{{ trunk_ports|map('extract', neighbour_details) }}"
为
neighbour_filtered:
Port1:
- host: SW1
platform: Switch
Port3:
- host: SW3
platform: Switch