Search code examples
xmlxpathansiblejunos-automation

How to get extract xml data and print debug msg


I am trying to extract 2 elements (l2-mac-address and l2-mac-logical-interface) via xpath and have them print as "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

Current Results:

"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"
}
]

How do I get the mac-address and it's interface to show up in the same debug: msg?

edit:

How would I get the mac and interface string list into a variable? I have tried adding this to the 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

But only get the last mac and interface added to the variable:

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

Solution

  • I have modified your playbook slightly, by introducing separate tasks to extract mac address and interface. This way you end up with 2 lists which can be zipped to get the desired output.

      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 }}"