如何获取提取 xml 数据并打印调试消息

How to get extract xml data and print debug msg

我正在尝试通过 xpath 提取 2 个元素(l2-mac-addressl2-mac-logical-interface)并将它们打印为“mac-address : 界面"

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和接口添加到变量中:

    TASK [Show Results] *************
ok: [lab960.ftc] => {
    "mac_list": [
        "60:03:47:17:fb:c7: xe-70/0/1.698"
    ]
}

我稍微修改了你的剧本,引入了单独的任务来提取 mac 地址和接口。通过这种方式,您最终会得到 2 个列表,可以将其压缩以获得所需的输出。

  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 address info
  xml:
    xmlstring: "{{ vpls_info.stdout }}"
    xpath: //l2-mac-entry/l2-mac-address
    content: text
  register: vpls_mac_address

- name: query mac interface info
  xml:
    xmlstring: "{{ vpls_info.stdout }}"
    xpath: //l2-mac-entry/l2-mac-logical-interface
    content: text
  register: vpls_mac_interface

- name: debug
  debug:
    msg: "{{ item[0]['l2-mac-address'] }}: {{ item[1]['l2-mac-logical-interface'] }}"
  loop: "{{ vpls_mac_address.matches | zip(vpls_mac_interface.matches) | list }}"