gpt4 book ai didi

ansible - 当每个匹配的键/值对时,在 Ansible 中合并列表

转载 作者:行者123 更新时间:2023-12-04 16:00:26 27 4
gpt4 key购买 nike

我有两个字典列表(port_info 和 int_trunk),当“端口”键的值匹配时,我试图合并它们,但并非每个端口都将在“int_trunk”中表示。在这种情况下,只需将“trunked_vlans”值留空即可。我正在使用“with_nested”来遍历列表。我似乎无法让输出做我想做的事。我已经尝试了 combine() 过滤器以及手动尝试创建变量但不能完全理解它。如何合并这两个列表?

---
- hosts: localhost
connection: local
gather_facts: no

vars:
trunk_ports: []
non_trunk_ports: []
new_port_info: []
port_info:
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/1"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/2"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: "Some Port"
duplex: auto
port: "Gi1/0/3"
speed: auto
status: notconnect
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 23
int_trunk:
- port: "Gi1/0/1"
vlans: "1-50"
- port: "Gi1/0/2"
vlans: "50-60"

这个输出应该是:

port_info: 
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/1"
speed: "a-1000"
status: connected
trunk_vlans: "1-50"
type: "10/100/1000BaseTX"
vlan: 3
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/2"
speed: "a-1000"
status: connected
trunk_vlans: "50-60"
type: "10/100/1000BaseTX"
vlan: 3
- desc: "Some Port"
duplex: auto
port: "Gi1/0/3"
speed: auto
status: notconnect
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 23

这是我尝试过的事情之一:

---
- hosts: localhost
connection: local
gather_facts: no

vars:
trunk_ports: []
non_trunk_ports: []
new_port_info: []
port_info:
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/1"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/2"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: " "
duplex: auto
port: "Gi1/0/3"
speed: auto
status: notconnect
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 23
int_trunk:
- port: "Gi1/0/1"
vlans: "1-50"
- port: "Gi1/0/2"
vlans: "50-60"

tasks:
- name: Merge trunk ports
set_fact:
trunk_ports: "{{ trunk_ports + [ { 'desc': item.0.desc, 'duplex': item.0.duplex, 'port': item.0.port, 'speed': item.0.speed, 'status
': item.0.status, 'trunk_vlans': item.1.vlans, 'type': item.0.type, 'vlan': item.0.vlan } ] }}"
with_nested:
- "{{ port_info }}"
- "{{ int_trunk }}"
when: item.0.port == item.1.port

- name: Merge non-trunk ports
set_fact:
non_trunk_ports: "{{ non_trunk_ports + [ { 'desc': item.0.desc, 'duplex': item.0.duplex, 'port': item.0.port, 'speed': item.0.speed,
'status': item.0.status, 'trunk_vlans': item.0.trunk_vlans, 'type': item.0.type, 'vlan': item.0.vlan } ] }}"
with_nested:
- "{{ port_info }}"
- "{{ int_trunk }}"
when: item.0.port != item.1.port

- name: Merge all ports
set_fact:
new_port_info: "{{ new_port_info + [item.0|combine(item.1)] }}"
with_nested:
- "{{ port_info }}"
- "{{ trunk_ports }}"
when: item.0.port == item.1.port

- name: Echo
debug: var=trunk_ports


- name: Echo
debug: var=non_trunk_ports

- name: Echo
debug: var=new_port_info

我最终得到端口 Gi1/0/1 和 Gi1/0/2,但没有 Gi1/0/3。

最佳答案

不确定是否有可以执行此操作的“一行”过滤。这是我想出的,首先解释流程:

  1. 我们使用 2 个 set_fact 步骤填充 2 个列表中所有公共(public)端口的列表。
  2. 我们将两个列表一起解析,当来自 port_info 的元素的端口属性等于来自 int_trunk 的元素时,我们合并/合并这两个字典。<
  3. 完成了公共(public)条目的合并。现在我们需要再次解析 port_info 列表,并将所有在 int_trunk 中没有匹配元素的元素添加到 port_info_final 列表中。

我们在 port_info_final 列表中获得了合并元素和唯一元素。

剧本:

---
- hosts: localhost
connection: local
gather_facts: false
vars:
trunk_ports: []
non_trunk_ports: []
new_port_info: []
port_info:
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/1"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: "*** Voice Server Port ***"
duplex: "a-full"
port: "Gi1/0/2"
speed: "a-1000"
status: connected
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 3
- desc: "Some Port"
duplex: auto
port: "Gi1/0/3"
speed: auto
status: notconnect
trunk_vlans: ""
type: "10/100/1000BaseTX"
vlan: 23
int_trunk:
- port: "Gi1/0/1"
vlans: "1-50"
- port: "Gi1/0/2"
vlans: "50-60"
port_info_final: []

tasks:

- name: get the lists of ports per list
set_fact:
portlist_1: "{{ port_info | map(attribute='port') | list }}"
portlist_2: "{{ int_trunk | map(attribute='port') | list }}"

- name: get the ports that exist in port_info but not in int_trunk
set_fact:
ports_not_in_int_trunk: "{{ portlist_1 | difference(portlist_2) }}"

- name: merge the dictionaries when the port is matched
set_fact:
port_info_final: "{{ port_info_final + [item[0] | combine(item[1])] }}"
when: item[0].port == item[1].port
loop: "{{ query('nested', int_trunk, port_info) }}"

- name: add all the port_info elements that dont have entry in int_trunk
set_fact:
port_info_final: "{{ port_info_final + [item] }}"
when: item.port in ports_not_in_int_trunk
loop: "{{ port_info }}"

- name: print results
debug:
msg: "{{ port_info_final }}"

结果:

TASK [print results] ************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
{
"desc": "*** Voice Server Port ***",
"duplex": "a-full",
"port": "Gi1/0/1",
"speed": "a-1000",
"status": "connected",
"trunk_vlans": "",
"type": "10/100/1000BaseTX",
"vlan": 3,
"vlans": "1-50"
},
{
"desc": "*** Voice Server Port ***",
"duplex": "a-full",
"port": "Gi1/0/2",
"speed": "a-1000",
"status": "connected",
"trunk_vlans": "",
"type": "10/100/1000BaseTX",
"vlan": 3,
"vlans": "50-60"
},
{
"desc": "Some Port",
"duplex": "auto",
"port": "Gi1/0/3",
"speed": "auto",
"status": "notconnect",
"trunk_vlans": "",
"type": "10/100/1000BaseTX",
"vlan": 23
}
]
}

PLAY RECAP **********************************************************************************************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0

希望对你有帮助

关于ansible - 当每个匹配的键/值对时,在 Ansible 中合并列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50662193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com