【问题标题】:Ansible assert match string from output来自输出的 Ansible 断言匹配字符串
【发布时间】:2021-07-06 10:04:22
【问题描述】:

我有一个剧本来检索 Cisco NX-OS 交换机的 show version 输出并使用 assert_module。

我想在输出中匹配一个字符串“NXOS: version 9.3(6)”。如果成功,则打印成功消息,否则打印失败消息。使用以下脚本,我总是收到失败消息。

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

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

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

【问题讨论】:

  • stdout 在您的结果中真的是一个空列表吗?这可以完美地解释为什么你从不匹配任何东西。您可以通过将您的值与修剪后的返回行进行比较 => "'NXOS: version 9.3(6)' in (show_version.stdout_lines | map('trim'))",轻松排除前/后未决空白问题。同时,我当然会尝试nxos_facts 模块并使用它的ansible_net_version gathered var
  • 我在上面尝试过,但仍然断言没有通过。 stdout 不是空的,它有很多与我无关的数据。我可以使用 nxos_facts,但这只是一个示例用例。实际上,我将需要其他显示输出数据,它们不是 nxos_facts 的一部分。致命:[LAB_TKYAT07LF03]:失败! => { "assertion": "'NXOS: version 9.3(6)' in (show_version.stdout_lines | map('trim'))", "changed": false, "evaluate_to": false, "msg": "Failed : 错误的操作系统"

标签: ansible cisco-ios nx-os cisco-nxos


【解决方案1】:

因为我目前无权访问 NX-OS 开关来执行

- name: Run show version on remote device
  cisco.nxos.nxos_command:
    commands: show version
  register: result

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

我已经根据你的完整输出准备了一个类似的测试

- 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),但只有一个元素。

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

发现它工作正常

TASK [Check NX-OS version] **************************************************************************************************************************************************
ok: [test1.example.com] => changed=false
  msg: 'Passed: All Leaf have current OS'

更改版本也失败

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-OS 交换机的信息,您可以从nxos_facts_module 中受益。请查看ansible_net_version 和可用的返回值。

- name: Gather only the config and default facts
  cisco.nxos.nxos_facts:
    gather_subset:
    - config

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

感谢

【讨论】:

    猜你喜欢
    • 2011-10-08
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多