From 0e05425b32505ffb1a588c053e05c7ba11e53f4b Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Tue, 3 Jul 2018 19:44:50 +0530 Subject: [PATCH] Refactor nxos cliconf get_device_info and add requirements for JSON structured output support in nxos_facts module (#42089) * nxos_facts requires JSON structured output support Signed-off-by: Trishna Guha * refactor get_device_info for legacy devices Signed-off-by: Trishna Guha * update additional logic Signed-off-by: Trishna Guha --- .../modules/network/nxos/nxos_facts.py | 11 ++++ lib/ansible/plugins/cliconf/nxos.py | 52 ++++++++++++++----- 2 files changed, 51 insertions(+), 12 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_facts.py b/lib/ansible/modules/network/nxos/nxos_facts.py index b08a204d75..f53d9e4fd5 100644 --- a/lib/ansible/modules/network/nxos/nxos_facts.py +++ b/lib/ansible/modules/network/nxos/nxos_facts.py @@ -37,6 +37,9 @@ description: author: - Jason Edelman (@jedelman8) - Gabriele Gerbino (@GGabriele) +notes: + - This module is only supported on the NX-OS device that supports JSON + structured output. NX-OS OS version should be 6.0(2)A8 or 7.x or later. options: gather_subset: description: @@ -169,6 +172,7 @@ vlan_list: import re from ansible.module_utils.network.nxos.nxos import run_commands, get_config +from ansible.module_utils.network.nxos.nxos import get_capabilities from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.six import string_types, iteritems @@ -538,6 +542,13 @@ def main(): module = AnsibleModule(argument_spec=spec, supports_check_mode=True) + capabilities = get_capabilities(module) + if capabilities: + os_version = capabilities['device_info']['network_os_version'] + os_version_major = int(os_version[0]) + if os_version_major < 7 and "6.0(2)A8" not in os_version: + module.fail_json(msg="this module requires JSON structured output support on the NX-OS device") + warnings = list() check_args(module, warnings) diff --git a/lib/ansible/plugins/cliconf/nxos.py b/lib/ansible/plugins/cliconf/nxos.py index 6c3a756555..23df41e7d7 100644 --- a/lib/ansible/plugins/cliconf/nxos.py +++ b/lib/ansible/plugins/cliconf/nxos.py @@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import json +import re from itertools import chain @@ -56,20 +57,47 @@ class Cliconf(CliconfBase): device_info = {} device_info['network_os'] = 'nxos' - reply = self.get('show version | json') - data = json.loads(reply) - platform_reply = self.get('show inventory | json') - platform_info = json.loads(platform_reply) + reply = self.get('show version') + platform_reply = self.get('show inventory') - device_info['network_os_version'] = data.get('sys_ver_str') or data.get('kickstart_ver_str') - device_info['network_os_model'] = data['chassis_id'] - device_info['network_os_hostname'] = data['host_name'] - device_info['network_os_image'] = data.get('isan_file_name') or data.get('kick_file_name') + match_sys_ver = re.search(r'\s+system:\s+version (\S+)', reply, re.M) + if match_sys_ver: + device_info['network_os_version'] = match_sys_ver.group(1) + else: + match_kick_ver = re.search(r'\s+kickstart:\s+version (\S+)', reply, re.M) + if match_kick_ver: + device_info['network_os_version'] = match_kick_ver.group(1) - inventory_table = platform_info['TABLE_inv']['ROW_inv'] - for info in inventory_table: - if 'Chassis' in info['name']: - device_info['network_os_platform'] = info['productid'] + if 'network_os_version' not in device_info: + match_sys_ver = re.search(r'\s+NXOS:\s+version (\S+)', reply, re.M) + if match_sys_ver: + device_info['network_os_version'] = match_sys_ver.group(1) + + match_chassis_id = re.search(r'Hardware\n\s+cisco\s+(\S+\s+\S+)', reply, re.M) + if match_chassis_id: + device_info['network_os_model'] = match_chassis_id.group(1) + + match_host_name = re.search(r'\s+Device name:\s+(\S+)', reply, re.M) + if match_host_name: + device_info['network_os_hostname'] = match_host_name.group(1) + + match_isan_file_name = re.search(r'\s+system image file is:\s+(\S+)', reply, re.M) + if match_isan_file_name: + device_info['network_os_image'] = match_isan_file_name.group(1) + else: + match_kick_file_name = re.search(r'\s+kickstart image file is:\s+(\S+)', reply, re.M) + if match_kick_file_name: + device_info['network_os_image'] = match_kick_file_name.group(1) + + if 'network_os_image' not in device_info: + match_isan_file_name = re.search(r'\s+NXOS image file is:\s+(\S+)', reply, re.M) + if match_isan_file_name: + device_info['network_os_image'] = match_isan_file_name.group(1) + + match_os_platform = re.search(r'NAME: "Chassis",\s+DESCR: "NX-OSv Chassis\s?"\s+\n' + r'PID:\s+(\S+)', platform_reply, re.M) + if match_os_platform: + device_info['network_os_platform'] = match_os_platform.group(1) return device_info