我如何使用ansible解析显示版本(cisco ios)中的元数据

How can I parse metadata from show version (cisco ios) using ansible

这是我的剧本 output.stdout 的 return 是一个列表,这就是为什么我用索引 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

这是 show_version.yaml 文件 我使用 regex101.com 检查,一切正常,但我得到所有键的空值。

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

这是输出 我想获取名称、型号、序列号、快速、千兆和虚拟接口的数量以及当然的版本


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\nexport@cisco.com.\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"
    ]
}


为什么我得到的是空值?

我想你必须设置导出选项:

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

如果没有这个指令,包含的变量和值将不会被发送回剧本进行进一步处理。

其次,我没有看到该实用程序包含以下内容:“{{ item }}”?

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

我建议你玩剧本 -vvv(非常冗长)来解决一些匹配问题

所以我看到你设置了导出到 playbook json_template,我觉得也是对的..

编辑

我认为 ^ 它不被视为行首

你能试试:regex: "\n(\\S+) uptime is"

我在 pattern_group @frenchy 中使用 export yes 并在 facts 和 它运作良好。你说得对,我不需要物品。

---
- 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"
      register: name

    - name: match IMAGE FILE
      pattern_match:
        regex: "System image file is (\S+)"
      register: image

    - name: match VIRTUAL INTERFACES
      pattern_match:
        regex: "^(\S+) Virtual Ethernet"
      register: virethint

    - name: match GIGABIT INTERFACES
      pattern_match:
        regex: "^(\S+) Gigabit Ethernet"
      register: gigethint

    - name: match FAST INTERFACES
      pattern_match:
        regex: "^(\S+) FastEthernet"
      register: fastethint

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

    - name: match MODEL NUMBER
      pattern_match:
        regex: "Model number.*: (\S+)"
      register: MODEL

    - name: match VERSION
      pattern_match:
        regex: "Cisco IOS Software, .+Version (\S+),"
      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