0

I'am trying to get the list of the device ID and the list of the port Id with ansible with regex but i get an empty list, below the output that i'am trying to parse it :

Device ID        Local Intrfce     Holdtme    Capability  Platform  Port ID
hello.fr.com #(this is in line separatly)
                 Fa 3/1/1         400             R S I    XXXX       Gi 3/3 #(and this in the next line)
cdp.fr.com
                 Fa 0/0/1         600            R S I     XXXX         Gi 3/3

Total cdp entries displayed : 2

and here my code:



tasks:
   - name: get neighbors
     ios_command:
       commands: 
         - show cdp neighbors
     register: output

   - set_fact: 
       reg_address: '(\S+[.]\S+[.]\S+[.]\S+)\s+'
       reg_ports: '\s+\S+\s\S+\s+\d+\s+\w\s\w\s\w\s+\S+\s+(\S+\s\d+[/]\d+)'

   - set_fact: 
       List_interfaces: []
       List_ports: []

   - set_fact: 
       List_interfaces: "{{List_interfaces + item | string | regex_search(reg_address, '\\1') }}"
     loop: "{{output.stdout_lines[0]}}"
     when: "{{ List_interfaces | length }} > 0"
   - set_fact: 
       List_ports: "{{List_ports + item | string | regex_search(reg_ports, '\\1') }}"
     loop: "{{ output.stdout_lines[0] }}"
     when: "{{ List_ports | length }} > 0"

1 Answers1

0

For example

    - set_fact:
        No_Of_Neighbors: "{{ output.stdout_lines[-1]['Total cdp entries displayed'] }}"
    - set_fact:
        cdp_neighbors: "{{ cdp_neighbors|default({})|
                           combine({Device_ID: {'Port_ID': Port_ID}}) }}"
      loop: "{{ range(0, No_Of_Neighbors|int)|list }}"
      vars:
        Device_ID: "{{ output.stdout_lines[item * 2 + 1] }}"
        params: "{{ output.stdout_lines[item * 2 + 2].split() }}"
        Port_ID: "{{ params[-2] }} {{ params[-1] }}"

gives

  cdp_neighbors:
    cdp.fr.com:
      Port_ID: Gi 3/3
    hello.fr.com:
      Port_ID: Gi 3/3

This parsing is not general. See show cdp neighbors.


Q: "Get the list of the device ID and the list of the port ID"

A: For example

    - set_fact:
        dev_ids: "{{ dev_ids|default([]) + [Device_ID] }}"
        port_ids: "{{ port_ids|default([]) + [Port_ID] }}"
      loop: "{{ range(0, No_Of_Neighbors|int)|list }}"

gives (using the same vars)

  dev_ids:
  - hello.fr.com
  - cdp.fr.com

  port_ids:
  - Gi 3/3
  - Gi 3/3

Q: "I did not undrestand this part:"

      vars:
        Device_ID: "{{ output.stdout_lines[item * 2 + 1] }}"
        params: "{{ output.stdout_lines[item * 2 + 2].split() }}"
        Port_ID: "{{ params[-2] }} {{ params[-1] }}"

A: The example below should be self-explaining

- hosts: localhost
  vars:
    output: [line0, line1, line2, line3, line4, line5, line6]
  tasks:
    - debug:
        msg: "{{ item }}
              {{ output[item * 2 + 1] }}
              {{ output[item * 2 + 2] }}"
      loop: "{{ range(0, 2)|list }}"

gives

ok: [localhost] => (item=0) => {
    "msg": "0 line1 line2"
}
ok: [localhost] => (item=1) => {
    "msg": "1 line3 line4"
}

The task below gives the same result

    - debug:
        msg: "{{ x }} {{ y }}  {{ z }}"
      loop: "{{ range(0, 2)|list }}"
      vars:
        x: "{{ item }}"
        y: "{{ output[item * 2 + 1] }}"
        z: "{{ output[item * 2 + 2] }}"

Note: As of 06/2021, I'm still not able to find a production parser of ios_commands. Devel only, for example ansible-ttp. Feel free to comment.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
Vladimir Botka
  • 5,138
  • 8
  • 20
  • Thank you very much @Vladimir but i didn't undrestand this part [ Device_ID: "{{ output.stdout_lines[item * 2 + 1] }}" params: "{{ output.stdout_lines[item * 2 + 2].split() }}" Port_ID: "{{ params[-2] }} {{ params[-1] }}" ] , i you can explain for me this part! thank u ! and with regex, is there a problem with the regex ? – user713502 Jun 03 '21 at 07:29
  • I've updated the answer with an example. It's out of the scope to explain Ansible's basics here. Open a new question and make it [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) if you have problems. Never put code into the comments. Edit your question instead and, still the same, make it [MRE](https://stackoverflow.com/help/minimal-reproducible-example). See [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Vladimir Botka Jun 03 '21 at 07:55
  • Thank you for the clarification is quiet clear for me now, just one problem he doesn't recognise the 'Total cdp entries displayed' despite it exists in the last line of the output – user713502 Jun 03 '21 at 08:50