summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrishna Guha <trishnaguha17@gmail.com>2017-11-13 11:42:14 +0000
committerGitHub <noreply@github.com>2017-11-13 11:42:14 +0000
commit4f333eff78249c4d8d1d8a8d74737ae2d29aea6b (patch)
tree338cead317f6262ec563f08e57a2a1048427becc
parentf8415adb1746db24bba85ec6cef7fa4e221a262f (diff)
downloadansible-4f333eff78249c4d8d1d8a8d74737ae2d29aea6b.tar.gz
nxos_interface error handling (#32846)
Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
-rw-r--r--CHANGELOG.md2
-rw-r--r--lib/ansible/module_utils/nxos.py5
-rw-r--r--lib/ansible/modules/network/nxos/nxos_interface.py141
3 files changed, 78 insertions, 70 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a28b52868b..a92802bb65 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -157,6 +157,8 @@ Ansible Changes By Release
https://github.com/ansible/ansible/issues/32764
* Better errors when loading malformed vault envelopes
(https://github.com/ansible/ansible/issues/28038)
+* nxos_interface error handling
+ (https://github.com/ansible/ansible/pull/32846)
<a id="2.4.1"></a>
diff --git a/lib/ansible/module_utils/nxos.py b/lib/ansible/module_utils/nxos.py
index 9af555e054..5c58fd26d4 100644
--- a/lib/ansible/module_utils/nxos.py
+++ b/lib/ansible/module_utils/nxos.py
@@ -34,7 +34,7 @@ from ansible.module_utils._text import to_text
from ansible.module_utils.basic import env_fallback, return_values
from ansible.module_utils.network_common import to_list, ComplexList
from ansible.module_utils.connection import exec_command
-from ansible.module_utils.six import iteritems
+from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils.urls import fetch_url
_DEVICE_CONNECTION = None
@@ -162,6 +162,9 @@ class Cli:
except ValueError:
out = to_text(out, errors='surrogate_then_replace').strip()
+ if item['output'] == 'json' and isinstance(out, string_types):
+ self._module.fail_json(msg='failed to retrieve output of %s in json format' % item['command'])
+
responses.append(out)
return responses
diff --git a/lib/ansible/modules/network/nxos/nxos_interface.py b/lib/ansible/modules/network/nxos/nxos_interface.py
index ef9d518938..cfe79fbdf7 100644
--- a/lib/ansible/modules/network/nxos/nxos_interface.py
+++ b/lib/ansible/modules/network/nxos/nxos_interface.py
@@ -275,75 +275,78 @@ def get_interface(intf, module):
try:
body = execute_show_command(command, module)[0]
except IndexError:
- body = []
-
+ return {}
if body:
- interface_table = body['TABLE_interface']['ROW_interface']
- if interface_table.get('eth_mode') == 'fex-fabric':
- module.fail_json(msg='nxos_interface does not support interfaces with mode "fex-fabric"')
- intf_type = get_interface_type(intf)
- if intf_type in ['portchannel', 'ethernet']:
- if not interface_table.get('eth_mode'):
- interface_table['eth_mode'] = 'layer3'
-
- if intf_type == 'ethernet':
- key_map.update(base_key_map)
- key_map.update(mode_map)
- temp_dict = apply_key_map(key_map, interface_table)
- temp_dict = apply_value_map(mode_value_map, temp_dict)
- interface.update(temp_dict)
-
- elif intf_type == 'svi':
- key_map.update(svi_map)
- temp_dict = apply_key_map(key_map, interface_table)
- interface.update(temp_dict)
- attributes = get_manual_interface_attributes(intf, module)
- interface['admin_state'] = str(attributes.get('admin_state',
- 'nxapibug'))
- interface['description'] = str(attributes.get('description',
- 'nxapi_bug'))
- command = 'show run interface {0}'.format(intf)
- body = execute_show_command(command, module)[0]
- if 'ip forward' in body:
- interface['ip_forward'] = 'enable'
- else:
- interface['ip_forward'] = 'disable'
- if 'fabric forwarding mode anycast-gateway' in body:
- interface['fabric_forwarding_anycast_gateway'] = True
- else:
- interface['fabric_forwarding_anycast_gateway'] = False
-
- elif intf_type == 'loopback':
- key_map.update(base_key_map)
- key_map.pop('admin_state')
- key_map.update(loop_map)
- temp_dict = apply_key_map(key_map, interface_table)
- if not temp_dict.get('description'):
- temp_dict['description'] = "None"
- interface.update(temp_dict)
-
- elif intf_type == 'management':
- key_map.update(base_key_map)
- temp_dict = apply_key_map(key_map, interface_table)
- interface.update(temp_dict)
-
- elif intf_type == 'portchannel':
- key_map.update(base_key_map)
- key_map.update(mode_map)
- temp_dict = apply_key_map(key_map, interface_table)
- temp_dict = apply_value_map(mode_value_map, temp_dict)
- if not temp_dict.get('description'):
- temp_dict['description'] = "None"
- interface.update(temp_dict)
-
- elif intf_type == 'nve':
- key_map.update(base_key_map)
- temp_dict = apply_key_map(key_map, interface_table)
- if not temp_dict.get('description'):
- temp_dict['description'] = "None"
- interface.update(temp_dict)
-
- interface['type'] = intf_type
+ try:
+ interface_table = body['TABLE_interface']['ROW_interface']
+ except KeyError:
+ return {}
+ if interface_table:
+ if interface_table.get('eth_mode') == 'fex-fabric':
+ module.fail_json(msg='nxos_interface does not support interfaces with mode "fex-fabric"')
+ intf_type = get_interface_type(intf)
+ if intf_type in ['portchannel', 'ethernet']:
+ if not interface_table.get('eth_mode'):
+ interface_table['eth_mode'] = 'layer3'
+
+ if intf_type == 'ethernet':
+ key_map.update(base_key_map)
+ key_map.update(mode_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ temp_dict = apply_value_map(mode_value_map, temp_dict)
+ interface.update(temp_dict)
+
+ elif intf_type == 'svi':
+ key_map.update(svi_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ interface.update(temp_dict)
+ attributes = get_manual_interface_attributes(intf, module)
+ interface['admin_state'] = str(attributes.get('admin_state',
+ 'nxapibug'))
+ interface['description'] = str(attributes.get('description',
+ 'nxapi_bug'))
+ command = 'show run interface {0}'.format(intf)
+ body = execute_show_command(command, module)[0]
+ if 'ip forward' in body:
+ interface['ip_forward'] = 'enable'
+ else:
+ interface['ip_forward'] = 'disable'
+ if 'fabric forwarding mode anycast-gateway' in body:
+ interface['fabric_forwarding_anycast_gateway'] = True
+ else:
+ interface['fabric_forwarding_anycast_gateway'] = False
+
+ elif intf_type == 'loopback':
+ key_map.update(base_key_map)
+ key_map.pop('admin_state')
+ key_map.update(loop_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ if not temp_dict.get('description'):
+ temp_dict['description'] = "None"
+ interface.update(temp_dict)
+
+ elif intf_type == 'management':
+ key_map.update(base_key_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ interface.update(temp_dict)
+
+ elif intf_type == 'portchannel':
+ key_map.update(base_key_map)
+ key_map.update(mode_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ temp_dict = apply_value_map(mode_value_map, temp_dict)
+ if not temp_dict.get('description'):
+ temp_dict['description'] = "None"
+ interface.update(temp_dict)
+
+ elif intf_type == 'nve':
+ key_map.update(base_key_map)
+ temp_dict = apply_key_map(key_map, interface_table)
+ if not temp_dict.get('description'):
+ temp_dict['description'] = "None"
+ interface.update(temp_dict)
+
+ interface['type'] = intf_type
return interface
@@ -372,7 +375,7 @@ def get_interfaces_dict(module):
try:
body = execute_show_command(command, module)[0]
except IndexError:
- body = {}
+ return {}
interfaces = {
'ethernet': [],