Home > Software design >  How can I parse metadata from show version (cisco ios) using ansible
How can I parse metadata from show version (cisco ios) using ansible

Time:10-29

Here is my playbook The return of output.stdout is a list that's why I call it by index of 0

- name: "GENERATE VERSION REPORT"
  hosts: all
  gather_facts: false

  roles:
    - ansible-network.network-engine

  tasks:
    - name: CAPTURE SHOW VERSION
      ios_command:
        commands:
          - show version
      register: output

    - name: Display output
      debug:
       var: output.stdout

    - name: PARSE THE RAW OUTPUT
      command_parser:
        file: "/home/nettools/ansible/parsers/show_version.yaml"
        content: "{{ output.stdout[0] }}"

    - name: DISPLAY THE PARSED DATA
      debug:
        var: version_facts

Here is the show_version.yaml file I use regex101.com to check and it is all ok but I get null value for all keys.

---
- name: parser meta data
  parser_metadata:
    version: 1.0
    command: show version
    network_os: cisco.ios.ios

- name: match version values
  pattern_group:
    - name: match name
      pattern_match:
        regex: "^(\\S ) uptime is"
        content: "{{ item }}"
      register: name

    - name: match IMAGE FILE
      pattern_match:
        regex: "System image file is (\\S )"
        content: "{{ item }}"
      register: image

    - name: match VIRTUAL INTERFACES
      pattern_match:
        regex: "^(\\S ) Virtual Ethernet interfaces"
        content: "{{ item }}"
      register: virethint

    - name: match GIGABIT INTERFACES
      pattern_match:
        regex: "^(\\S ) Gigabit Ethernet interfaces"
        content: "{{ item }}"
      register: gigethint

    - name: match FAST INTERFACES
      pattern_match:
        regex: "^(\\S ) FastEthernet interfaces"
        content: "{{ item }}"
      register: fastethint

    - name: match SERIAL NUMBER
      pattern_match:
        regex: "System serial number.*: (\\S )"
        content: "{{ item }}"
      register: SN

    - name: match MODEL NUMBER
      pattern_match:
        regex: "Model number.*: (\\S )"
        content: "{{ item }}"
      register: MODEL

    - name: match VERSION
      pattern_match:
        regex: "Cisco IOS Software, . Version (\\S ),"
        content: "{{ item }}"
      register: version

- name: generate json data structure
  json_template:
    template:
      - key: "{{ item.name.matches.0 }}"
        object:
        - key: element
          object:
            - key: name
              value: "{{ item.name.matches.0 }}"
            - key: image
              value: "{{ item.image.matches.0 }}"
            - key: virethint
              value: "{{ item.virethint.matches.0 }}"
            - key: gigethint
              value: "{{ item.gigethint.matches.0 }}"
            - key: fastethint
              value: "{{ item.fastethint.matches.0 }}"
            - key: SN
              value: "{{ item.SN.matches.0 }}"
            - key: MODEL
              value: "{{ item.MODEL.matches.0 }}"
            - key: version
              value: "{{ item.version.matches.0 }}"
  export: yes
  register: version_facts

Here is the output I want to get the name, the model, the serial number, the number of fast, giga and virtual interfaces and the version of course


task path: /home/nettools/ansible/playbook/cisco_playbook/sh_ver_parsed.yml:19
<10.224.12.4> attempting to start connection
<10.224.12.4> using connection plugin ansible.netcommon.network_cli
<10.224.12.4> found existing local domain socket, using it!
<10.224.12.4> updating play_context for connection
<10.224.12.4>
<10.224.12.4> local domain socket path is /root/.ansible/pc/5d7175bc45
processing directive: parser meta data
command_parser: using parser version 1.0
processing directive: match version values
command_parser: starting pattern_match [match name] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match IMAGE FILE] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match VIRTUAL INTERFACES] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match GIGABIT INTERFACES] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match FAST INTERFACES] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match SERIAL NUMBER] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match MODEL NUMBER] in pattern_group
command_parser: loop_var is item
command_parser: starting pattern_match [match VERSION] in pattern_group
command_parser: loop_var is item
processing directive: generate json data structure
ok: [ITG15019] => {
    "ansible_facts": {
        "version_facts": {
            "null": {
                "element": {
                    "MODEL": null,
                    "SN": null,
                    "fastethint": null,
                    "gigethint": null,
                    "image": null,
                    "name": null,
                    "version": null,
                    "virethint": null
                }
            }
        }
    },
    "changed": false,
    "included": [
        "/home/nettools/ansible/parsers/show_version.yaml"
    ]
}
ok: [ITG15019] => {
    "output.stdout": [
        "Cisco IOS Software, C2960X Software (C2960X-UNIVERSALK9-M), Version 15.0(2)EX1, RELEASE SOFTWARE (fc1)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2013 by Cisco Systems, Inc.\nCompiled Fri 28-Jun-13 13:20 by prod_rel_team\n\nROM: Bootstrap program is Alpha board boot loader\nBOOTLDR: C2960X Boot Loader (C2960X-HBOOT-M) Version 15.0(2r)EX, RELEASE SOFTWARE (fc1)\n\nDTY-W0I030 uptime is 2 weeks, 3 days, 16 hours, 28 minutes\nSystem returned to ROM by power-on\nSystem restarted at 22:34:39 cet Fri Oct 8 2021\nSystem image file is \"flash:/c2960x-universalk9-mz.150-2.EX1/c2960x-universalk9-mz.150-2.EX1.bin\"\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export, transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\[email protected].\n\ncisco WS-C2960X-48LPS-L (APM86XXX) processor (revision V01) with 524288K bytes of memory.\nProcessor board ID FOC1728W1W1\nLast reset from power-on\n2 Virtual Ethernet interfaces\n1 FastEthernet interface\n156 Gigabit Ethernet interfaces\nThe password-recovery mechanism is enabled.\n\n512K bytes of flash-simulated non-volatile configuration memory.\nBase ethernet MAC Address       : F8:4F:57:47:EA:00\nMotherboard assembly number     : 73-14191-03\nPower supply part number        : 341-0528-01\nMotherboard serial number       : FOC172615CP\nPower supply serial number      : LIT17230R2W\nModel revision number           : V01\nMotherboard revision number     : A0\nModel number                    : WS-C2960X-48LPS-L\nDaughterboard assembly number   : 73-14200-03\nDaughterboard serial number     : FOC172635D1\nSystem serial number            : FOC1728W1W1\nTop Assembly Part Number        : 800-38006-01\nTop Assembly Revision Number    : A0\nVersion ID                      : V01\nCLEI Code Number                : CMMLP00ARA\nDaughterboard revision number   : A0\nHardware Board Revision Number  : 0x05\n\n\nSwitch Ports Model              SW Version            SW Image                 \n------ ----- -----              ----------            ----------               \n*    1 52    WS-C2960X-48LPS-L  15.0(2)EX1            C2960X-UNIVERSALK9-M     \n     2 52    WS-C2960X-48LPS-L  15.0(2)EX1            C2960X-UNIVERSALK9-M     \n     3 52    WS-C2960X-48LPS-L  15.0(2)EX1            C2960X-UNIVERSALK9-M     \n\n\nSwitch 02\n---------\nSwitch Uptime                   : 2 weeks, 3 days, 16 hours, 29 minutes \nBase ethernet MAC Address       : 50:17:FF:F8:E7:00\nMotherboard assembly number     : 73-14191-03\nPower supply part number        : 341-0528-01\nMotherboard serial number       : FOC17330K1C\nPower supply serial number      : LIT17290RVF\nModel revision number           : A0\nMotherboard revision number     : A0\nModel number                    : WS-C2960X-48LPS-L\nDaughterboard assembly number   : 73-14200-03\nDaughterboard serial number     : FOC173135JL\nSystem serial number            : FOC1733S1EA\nTop assembly part number        : 800-38006-01\nTop assembly revision number    : A0\nVersion ID                      : V01\nCLEI Code Number                : CMMLP00ARA\nDaughterboard revision number   : A0\n\nSwitch 03\n---------\nSwitch Uptime                   : 2 weeks, 3 days, 16 hours, 29 minutes \nBase ethernet MAC Address       : 50:17:FF:83:F2:00\nMotherboard assembly number     : 73-14191-03\nPower supply part number        : 341-0528-01\nMotherboard serial number       : FOC17330K9R\nPower supply serial number      : LIT17290RLR\nModel revision number           : A0\nMotherboard revision number     : A0\nModel number                    : WS-C2960X-48LPS-L\nDaughterboard assembly number   : 73-14200-03\nDaughterboard serial number     : FOC173134P7\nSystem serial number            : FOC1733S1EP\nTop assembly part number        : 800-38006-01\nTop assembly revision number    : A0\nVersion ID                      : V01\nCLEI Code Number                : CMMLP00ARA\nDaughterboard revision number   : A0\n\nConfiguration register is 0xF"
    ]
}


Why I am getting null value ?

CodePudding user response:

i think you have to set the option export:

- name: match version values
  pattern_group:
    - name: match name
      pattern_match:
        regex: "^(\\S ) uptime is"
        content: "{{ item }}"
      register: name
      export: yes

Without this directive the variable and value contained will not be sent back to the playbook for further processing.

Second, i dont see the utility to have content: "{{ item }}"?

- name: match version values
  pattern_group:
    - name: match name
      pattern_match:
        regex: "^(\\S ) uptime is"
      register: name

i suggest you to play the playbook with -vvv (very verbose) to trap some matching problems

so i see you have set export to playbook json_template, i think is right too..

EDIT

i think ^ its not seen as start of line

could you try: regex: "\n(\\S ) uptime is"

CodePudding user response:

I use the export yes in the pattern_group @frenchy and register the value in facts and it is working well. You have right I don't need item.

---
- name: parser meta data
  parser_metadata:
    version: 1.0
    command: show version
    network_os: cisco.ios.ios

- name: match version values
  pattern_group:
    - name: match name
      pattern_match:
        regex: "\n(\\S ) uptime is"
        content: "{{ item }}"
      register: name

    - name: match IMAGE FILE
      pattern_match:
        regex: "System image file is (\\S )"
        content: "{{ item }}"
      register: image

    - name: match VIRTUAL INTERFACES
      pattern_match:
        regex: "^(\\S ) Virtual Ethernet interfaces"
        content: "{{ item }}"
      register: virethint

    - name: match GIGABIT INTERFACES
      pattern_match:
        regex: "^(\\S ) Gigabit Ethernet interfaces"
        content: "{{ item }}"
      register: gigethint

    - name: match FAST INTERFACES
      pattern_match:
        regex: "^(\\S ) FastEthernet interfaces"
        content: "{{ item }}"
      register: fastethint

    - name: match SERIAL NUMBER
      pattern_match:
        regex: "System serial number.*: (\\S )"
        content: "{{ item }}"
      register: SN

    - name: match MODEL NUMBER
      pattern_match:
        regex: "Model number.*: (\\S )"
        content: "{{ item }}"
      register: MODEL

    - name: match VERSION
      pattern_match:
        regex: "Cisco IOS Software, . Version (\\S ),"
        content: "{{ item }}"
      register: version
  register: facts
  export: yes

- name: generate json data structure
  json_template:
    template:
      - key: "{{ facts.name.matches.0 }}"
        object:
        - key: element
          object:
            - key: name
              value: "{{ facts.name.matches.0 }}"
            - key: image
              value: "{{ facts.image.matches.0 }}"
            - key: virethint
              value: "{{ facts.virethint.matches.0 }}"
            - key: gigethint
              value: "{{ facts.gigethint.matches.0 }}"
            - key: fastethint
              value: "{{ facts.fastethint.matches.0 }}"
            - key: SN
              value: "{{ facts.SN.matches.0 }}"
            - key: MODEL
              value: "{{ facts.MODEL.matches.0 }}"
            - key: version
              value: "{{ facts.version.matches.0 }}"
  export: yes
  register: version_facts
  • Related