这是Python代码-我想在YAML和/或Jinja2的Ansible剧本中“编写相同的代码”(获得相同的结果),而无需使用外部模块。
arp_interfaces = {
"3.3.3.3": "eth0",
"4.4.4.4": "eth1",
}
route_interfaces = [
{
"interface": "eth0",
"next_hop_ip": "3.3.3.3",
"unreachable": ""
},
{
"interface": "eth2",
"next_hop_ip": "4.4.4.4",
"unreachable": ""
}
]
different_interfaces = {}
for arp_ip, arp_iface in arp_interfaces.items():
for route in route_interfaces:
if arp_ip == route['next_hop_ip']:
if arp_iface != route['interface']:
different_interfaces[arp_ip] = {"arp": arp_iface, "ip_route": route["interface"]}
print(different_interfaces)
输出:
C:\Python373\python.exe D:/projects/Python/python-cli/main.py
{'4.4.4.4': {'arp': 'eth1', 'ip_route': 'eth2'}}
答案 0 :(得分:1)
以下任务
- set_fact:
different_interfaces: "{{ different_interfaces|default({})|
combine({item.0.key: {'arp': item.0.value,
'ip_route': item.1.interface}})
}}"
loop: "{{ arp_interfaces|dict2items|product(route_interfaces)|list }}"
when:
- item.0.key == item.1.next_hop_ip
- item.0.value != item.1.interface
- debug:
var: different_interfaces
给予
"different_interfaces": {
"4.4.4.4": {
"arp": "eth1",
"ip_route": "eth2"
}
}