Я пытаюсь извлечь 2 элемента (l2-mac-address
и l2-mac-logical-interface
) через xpath и распечатать их как " 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>
Playbook:
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
?
edit:
Как мне получить список строк mac и interface в переменную? Я попытался добавить это в playbook:
- 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 и интерфейс, добавленный к переменной:
TASK [Show Results] *************
ok: [lab960.ftc] => {
"mac_list": [
"60:03:47:17:fb:c7: xe-70/0/1.698"
]
}