首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出中的不可断言匹配字符串

输出中的不可断言匹配字符串
EN

Stack Overflow用户
提问于 2021-07-06 10:04:22
回答 1查看 1K关注 0票数 1

我有一个游戏手册,可以从Cisco交换机中检索show version输出,并使用assert_module。

我想在输出中匹配一个字符串"NXOS: Version9.3(6)“。如果成功,则打印成功消息,否则打印失败消息。在下面的脚本中,我总是收到失败的消息。

代码语言:javascript
复制
--- 
- 
  connection: network_cli
  gather_facts: false
  hosts: LAB_LF
  tasks:
    - 
      name: "show commands"
      nxos_command: 
        commands: 
        - command: show version
      register: show_version
      ignore_errors: true

    - 
      debug: 
        msg: "{{ show_version }}"
      name: "display the output from switch"
    - 
      assert: 
        that:
          - "'NXOS: version 9.3(6)' in show_version.stdout"
        success_msg: "Passed: All Leaf have current OS"
        fail_msg: "Failed: wrong os"
      ignore_errors: yes
      when: inventory_hostname in groups['LAB_LF0304']
      name: "assert the output from switch"

开关的打印输出如下所示。由于数据结构很重要,所以我正在粘贴完整的显示输出。

代码语言:javascript
复制
TASK [display the output from switch] *******************************************************************************************
ok: [LAB_LF03] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "stdout": [
            "Cisco Nexus Operating System (NX-OS) Software\nTAC support: http://www.cisco.com/tac\nCopyright (C) 2002-2020, Cisco and/or its affiliates.\nAll rights reserved.\nThe copyrights to certain works contained in this software are\nowned by other third parties and used and distributed under their own\nlicenses, such as open source.  This software is provided \"as is,\" and unless\notherwise stated, there is no warranty, express or implied, including but not\nlimited to warranties of merchantability and fitness for a particular purpose.\nCertain components of this software are licensed under\nthe GNU General Public License (GPL) version 2.0 or \nGNU General Public License (GPL) version 3.0  or the GNU\nLesser General Public License (LGPL) Version 2.1 or \nLesser General Public License (LGPL) Version 2.0. \nA copy of each such license is available at\nhttp://www.opensource.org/licenses/gpl-2.0.php and\nhttp://opensource.org/licenses/gpl-3.0.html and\nhttp://www.opensource.org/licenses/lgpl-2.1.php and\nhttp://www.gnu.org/licenses/old-licenses/library.txt.\n\nSoftware\n  BIOS: version 05.42\n NXOS: version 9.3(6)\n  BIOS compile time:  06/14/2020\n  NXOS image file is: bootflash:///nxos.9.3.6.bin\n  NXOS compile time:  11/9/2020 23:00:00 [11/10/2020 20:00:21]\n\n\nHardware\n  cisco Nexus9000 C9336C-FX2 Chassis \n  Intel(R) Xeon(R) CPU D-1526 @ 1.80GHz with 24569968 kB of memory.\n  Processor Board ID FDO2449113F\n\n  Device name: LAB-LF03\n  bootflash:  115805708 kB\nKernel uptime is 82 day(s), 20 hour(s), 0 minute(s), 44 second(s)\n\nLast reset at 832054 usecs after Thu Apr 15 13:33:53 2021\n  Reason: Reset due to upgrade\n  System version: 9.2(3)\n  Service: \n\nplugin\n  Core Plugin, Ethernet Plugin\n\nActive Package(s):"
        ],
        "stdout_lines": [
            [
                "Cisco Nexus Operating System (NX-OS) Software",
                "TAC support: http://www.cisco.com/tac",
                "Copyright (C) 2002-2020, Cisco and/or its affiliates.",
                "All rights reserved.",
                "The copyrights to certain works contained in this software are",
                "owned by other third parties and used and distributed under their own",
                "licenses, such as open source.  This software is provided \"as is,\" and unless",
                "otherwise stated, there is no warranty, express or implied, including but not",
                "limited to warranties of merchantability and fitness for a particular purpose.",
                "Certain components of this software are licensed under",
                "the GNU General Public License (GPL) version 2.0 or ",
                "GNU General Public License (GPL) version 3.0  or the GNU",
                "Lesser General Public License (LGPL) Version 2.1 or ",
                "Lesser General Public License (LGPL) Version 2.0. ",
                "A copy of each such license is available at",
                "http://www.opensource.org/licenses/gpl-2.0.php and",
                "http://opensource.org/licenses/gpl-3.0.html and",
                "http://www.opensource.org/licenses/lgpl-2.1.php and",
                "http://www.gnu.org/licenses/old-licenses/library.txt.",
                "",
                "Software",
                "  BIOS: version 05.42",
                " NXOS: version 9.3(6)",
                "  BIOS compile time:  06/14/2020",
                "  NXOS image file is: bootflash:///nxos.9.3.6.bin",
                "  NXOS compile time:  11/9/2020 23:00:00 [11/10/2020 20:00:21]",
                "",
                "",
                "Hardware",
                "  cisco Nexus9000 C9336C-FX2 Chassis ",
                "  Intel(R) Xeon(R) CPU D-1526 @ 1.80GHz with 24569968 kB of memory.",
                "  Processor Board ID ",
                "",
                "  Device name: LAB-LF03",
                "  bootflash:  115805708 kB",
                "Kernel uptime is 82 day(s), 20 hour(s), 0 minute(s), 44 second(s)",
                "",
                "Last reset at 832054 usecs after Thu Apr 15 13:33:53 2021",
                "  Reason: Reset due to upgrade",
                "  System version: 9.2(3)",
                "  Service: ",
                "",
                "plugin",
                "  Core Plugin, Ethernet Plugin",
                "",
                "Active Package(s):"
            ]
        ]
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-11-06 18:11:51

因为我目前无法访问NX交换机来执行

代码语言:javascript
复制
- name: Run show version on remote device
  cisco.nxos.nxos_command:
    commands: show version
  register: result

- name: Show result
  debug:
    msg: "{{ result.stdout_lines }}"

我已经准备了一个类似的测试,基于您的全部输出

代码语言:javascript
复制
- name: Set result value
  set_fact:
    result:
      stdout_lines: [
        [
          "Software",
          "  BIOS: version 05.42",
          " NXOS: version 9.3(6)",
          "  BIOS compile time:  06/14/2020",
          "  NXOS image file is: bootflash:///nxos.9.3.6.bin",
          "  NXOS compile time:  11/9/2020 23:00:00 [11/10/2020 20:00:21]"
        ]
      ]

它看起来像一个list with list(s),但是只有一个元素。

代码语言:javascript
复制
- name: Show result
  debug:
    msg: "{{ result.stdout_lines[0] }}"

- name: Check NX-OS version
  assert:
    that:
      - "'NXOS: version 9.3(6)' in (result.stdout_lines[0] | trim ('trim') )"
    success_msg: "Passed: All Leaf have current OS"
    fail_msg: "Failed: wrong os"

发现它起作用了

代码语言:javascript
复制
TASK [Check NX-OS version] **************************************************************************************************************************************************
ok: [test1.example.com] => changed=false
  msg: 'Passed: All Leaf have current OS'

同样,更改版本也失败了

代码语言:javascript
复制
TASK [Show result] *********************************************************************************************************************************************
ok: [test1.example.com] =>
  msg:
  - Software
  - '  BIOS: version 05.42'
  - ' NXOS: version 9.2(5)'
  - '  BIOS compile time:  06/14/2020'
  - '  NXOS image file is: bootflash:///nxos.9.2.5.bin'
  - '  NXOS compile time:  11/9/2020 23:00:00 [11/10/2020 20:00:21]'

TASK [Check NX-OS version] **************************************************************************************************************************************************
fatal: [test1.example.com]: FAILED! => changed=false
  assertion: '''NXOS: version 9.3(6)'' in (result.stdout_lines[0] | map(''trim''))'
  evaluated_to: false
  msg: 'Failed: wrong os'

正如Zeitounator已经提到的,要了解NX交换机的实际情况,您可以利用nxos_facts_module。请查看ansible_net_version和可用的返回值。

代码语言:javascript
复制
- name: Gather only the config and default facts
  cisco.nxos.nxos_facts:
    gather_subset:
    - config

- name: Show facts
  debug:
    msg: "{{ ansible_facts.net_version }}"

多亏了

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68268501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档