From a7421e86116361b41790d663fed065ade35af54e Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Tue, 29 May 2018 21:43:34 +0530 Subject: [PATCH] httpapi fix nxos (#40806) * httpapi fix nxos Signed-off-by: Trishna Guha * nxos_hsrp fix Signed-off-by: Trishna Guha --- lib/ansible/modules/network/nxos/nxos_hsrp.py | 27 +++++-------------- lib/ansible/plugins/cliconf/eos.py | 5 +++- lib/ansible/plugins/cliconf/nxos.py | 5 +++- lib/ansible/plugins/httpapi/nxos.py | 4 +++ 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_hsrp.py b/lib/ansible/modules/network/nxos/nxos_hsrp.py index 734c6346397..a108fbe56ca 100644 --- a/lib/ansible/modules/network/nxos/nxos_hsrp.py +++ b/lib/ansible/modules/network/nxos/nxos_hsrp.py @@ -160,21 +160,6 @@ PARAM_TO_DEFAULT_KEYMAP = { } -def execute_show_command(command, module): - device_info = get_capabilities(module) - network_api = device_info.get('network_api', 'nxapi') - - if network_api == 'cliconf': - command += ' | json' - cmds = [command] - body = run_commands(module, cmds) - elif network_api == 'nxapi': - cmds = [command] - body = run_commands(module, cmds) - - return body - - def apply_key_map(key_map, table): new_dict = {} for key in table: @@ -189,11 +174,11 @@ def apply_key_map(key_map, table): def get_interface_mode(interface, intf_type, module): - command = 'show interface {0}'.format(interface) + command = 'show interface {0} | json'.format(interface) interface = {} mode = 'unknown' try: - body = execute_show_command(command, module)[0] + body = run_commands(module, [command])[0] except IndexError: return None @@ -208,7 +193,7 @@ def get_interface_mode(interface, intf_type, module): def get_hsrp_group(group, interface, module): - command = 'show hsrp group {0} all'.format(group) + command = 'show hsrp group {0} all | json'.format(group) hsrp = {} hsrp_key = { @@ -224,9 +209,9 @@ def get_hsrp_group(group, interface, module): } try: - body = execute_show_command(command, module)[0] + body = run_commands(module, [command])[0] hsrp_table = body['TABLE_grp_detail']['ROW_grp_detail'] - except (AttributeError, IndexError, TypeError): + except (AttributeError, IndexError, TypeError, KeyError): return {} if isinstance(hsrp_table, dict): @@ -349,7 +334,7 @@ def is_default(interface, module): command = 'show run interface {0}'.format(interface) try: - body = execute_show_command(command, module)[0] + body = run_commands(module, [command], check_rc=False)[0] if 'invalid' in body.lower(): return 'DNE' else: diff --git a/lib/ansible/plugins/cliconf/eos.py b/lib/ansible/plugins/cliconf/eos.py index 22239cac5c2..5a8e9736206 100644 --- a/lib/ansible/plugins/cliconf/eos.py +++ b/lib/ansible/plugins/cliconf/eos.py @@ -94,8 +94,11 @@ class Cliconf(CliconfBase): def get_capabilities(self): result = {} result['rpc'] = self.get_base_rpc() - result['network_api'] = 'cliconf' result['device_info'] = self.get_device_info() + if isinstance(self._connection, NetworkCli): + result['network_api'] = 'cliconf' + else: + result['network_api'] = 'eapi' return json.dumps(result) # Imported from module_utils diff --git a/lib/ansible/plugins/cliconf/nxos.py b/lib/ansible/plugins/cliconf/nxos.py index 01e492717e0..5172ac751c2 100644 --- a/lib/ansible/plugins/cliconf/nxos.py +++ b/lib/ansible/plugins/cliconf/nxos.py @@ -95,8 +95,11 @@ class Cliconf(CliconfBase): def get_capabilities(self): result = {} result['rpc'] = self.get_base_rpc() - result['network_api'] = 'cliconf' result['device_info'] = self.get_device_info() + if isinstance(self._connection, NetworkCli): + result['network_api'] = 'cliconf' + else: + result['network_api'] = 'nxapi' return json.dumps(result) # Migrated from module_utils diff --git a/lib/ansible/plugins/httpapi/nxos.py b/lib/ansible/plugins/httpapi/nxos.py index 2a49257823a..d6cfc4fe1d3 100644 --- a/lib/ansible/plugins/httpapi/nxos.py +++ b/lib/ansible/plugins/httpapi/nxos.py @@ -89,9 +89,13 @@ class HttpApi: out = to_text(exc) out = to_list(out) + if not out[0]: + return out + for index, response in enumerate(out): if response[0] == '{': out[index] = json.loads(response) + return out