1

I have a playbook to retrieve show version output from a Cisco NX-OS switch and using assert_module.

I want to match a string "NXOS: version 9.3(6)" in the output. If succeeds, then print success message, or else fail message. With the below scripts I am always getting failed message.

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

Printed output from the switch is below. As the data structure is important, I am pasting full displayed output

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):"
            ]
        ]
    }
}
U880D
  • 8,601
  • 6
  • 24
  • 40
har83
  • 33
  • 1
  • 8
  • 2
    Is `stdout` really an empty list in your result ? That would perfectly explain why you never match anything. You can easily rule out pre/post pending white spaces problems by comparing your value against the trimmed returned lines => `"'NXOS: version 9.3(6)' in (show_version.stdout_lines | map('trim'))"`. Meanwhile, I would certainly give a try to the `nxos_facts` module and use its [`ansible_net_version` gathered var](https://docs.ansible.com/ansible/latest/collections/cisco/nxos/nxos_facts_module.html#return-ansible_net_version) – Zeitounator Jul 06 '21 at 10:58
  • I tried with above and still assert did not pass. stdout is not empty, it has lots of data, that are irrelevant to me. I could use nxos_facts, but this is just an example use case. In realiity i will need other show output data, which are not part of nxos_facts. fatal: [LAB_TKYAT07LF03]: FAILED! => { "assertion": "'NXOS: version 9.3(6)' in (show_version.stdout_lines | map('trim'))", "changed": false, "evaluated_to": false, "msg": "Failed: wrong os" – har83 Jul 07 '21 at 00:37

1 Answers1

0

Since I don't have access to a NX-OS switch currently to perform

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

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

I've prepared a somehow similar test based on your full output via

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

It looks like a list with list(s), but with one element only.

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

and found it working

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

as well failing for a changed version

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'

As Zeitounator already mentioned, to gets facts about NX-OS switches you may take advantage from nxos_facts_module. Please have a look into ansible_net_version and the available return values.

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

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

Thanks to

U880D
  • 8,601
  • 6
  • 24
  • 40