我正在尝试通过 xpath 提取 2 个元素(l2-mac-address
和l2-mac-logical-interface
)并将它们打印为“ mac-address:interface ”
XML:
<l2ald-rtb-macdb>
<l2ald-mac-entry junos:style="brief-rtb">
<l2-mac-routing-instance>lab-ppp</l2-mac-routing-instance>
<l2-mac-bridging-domain>__lab-ppp__</l2-mac-bridging-domain>
<l2-bridge-vlan>none</l2-bridge-vlan>
<l2-mac-entry>
<l2-mac-address>28:c0:da:6c:43:38</l2-mac-address>
<l2-mac-flags>D</l2-mac-flags>
<l2-mac-logical-interface>ge-11/2/2.698</l2-mac-logical-interface>
</l2-mac-entry>
<l2-mac-entry>
<l2-mac-address>60:03:47:07:c8:96</l2-mac-address>
<l2-mac-flags>D</l2-mac-flags>
<l2-mac-logical-interface>xe-70/0/0.698</l2-mac-logical-interface>
</l2-mac-entry>
<l2-mac-entry>
<l2-mac-address>60:03:47:09:6b:51</l2-mac-address>
<l2-mac-flags>D</l2-mac-flags>
<l2-mac-logical-interface>xe-70/0/0.698</l2-mac-logical-interface>
</l2-mac-entry>
<l2-mac-entry>
<l2-mac-address>60:03:47:17:fb:c7</l2-mac-address>
<l2-mac-flags>D</l2-mac-flags>
<l2-mac-logical-interface>xe-70/0/1.698</l2-mac-logical-interface>
</l2-mac-entry>
</l2ald-mac-entry>
</l2ald-rtb-macdb>
剧本:
tasks:
- name: RUN RPC Command
juniper_junos_rpc:
rpcs:
- get-vpls-mac-table
kwargs:
instance: lab-ppp
provider: "{{ credentials }}"
register: vpls_info
- name: query mac info
xml:
xmlstring: "{{ vpls_info.stdout }}"
xpath: //l2-mac-entry/l2-mac-address | //l2-mac-entry/l2-mac-logical-interface
content: text
register: vpls_macs
- name: show results
debug:
var: vpls_macs.matches
当前结果:
"vpls_macs.matches": [
{
"l2-mac-address": "28:c0:da:6c:43:38"
},
{
"l2-mac-logical-interface": "ge-11/2/2.698"
},
{
"l2-mac-address": "60:03:47:07:c8:96"
},
{
"l2-mac-logical-interface": "xe-70/0/0.698"
}
]
我如何获得mac地址和它的界面显示在同一个debug: msg
?
编辑:
我如何将 mac 和接口字符串列表放入变量中?我尝试将其添加到剧本中:
- name: Build List
set_fact:
mac_list: "{{ mac_list|default([]) }} + [ '{{item[0]['l2-mac-address'] }}: {{ item[1]['l2-mac-logical-interface'] }}' ]"
loop: "{{ vpls_mac_address.matches | zip(vpls_mac_interface.matches) | list }}"
- name: Show Results
debug:
var: mac_list
但只获取最后一个 mac 和 interface 添加到变量中:
TASK [Show Results] *************
ok: [lab960.ftc] => {
"mac_list": [
"60:03:47:17:fb:c7: xe-70/0/1.698"
]
}