Repository: ansible/ansible

Issue:
nxos_interface errors out with KeyError: 'eth_mode' 
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
 - nxos_interface

##### NXOS VERSION
7.0(3)I2(5)

##### ANSIBLE VERSION
```
ansible 2.4.0 (devel 3251aecd95) last updated 2017/08/29 09:19:59 (GMT -400)
  config file = None
  configured module search path = [u'/Users/mwiebe/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/mwiebe/Projects/nxos_ansible/fix_ansible/lib/ansible
  executable location = /Users/mwiebe/Projects/nxos_ansible/fix_ansible/bin/ansible
  python version = 2.7.13 (default, Apr  4 2017, 08:47:57) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)]
```

##### OS / ENVIRONMENT
Ubuntu 14.04

##### SUMMARY
A check was added recently to indicate that interfaces with mode fex-fabric are not supported. The field that is checked for the setting may or may not be available depending on the state of the interface.  If it's not the following key error will be generated. 

```shell
TASK [nxos_interface : set state=absent] *******************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'eth_mode'
fatal: [n9k.example.com]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_MBC73S/ansible_module_nxos_interface.py\", line 713, in <module>\n    main()\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_MBC73S/ansible_module_nxos_interface.py\", line 639, in main\n    existing, is_default = smart_existing(module, intf_type, normalized_interface)\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_MBC73S/ansible_module_nxos_interface.py\", line 535, in smart_existing\n    existing = get_interface(normalized_interface, module)\n  File \"/var/folders/ww/538d42ys18l5tjyz0l1yk1rc0000gn/T/ansible_MBC73S/ansible_module_nxos_interface.py\", line 283, in get_interface\n    if interface_table['eth_mode'] == 'fex-fabric':\nKeyError: 'eth_mode'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
	to retry, use: --limit @/Users/mwiebe/Projects/nxos_ansible/fix_ansible/test/integration/nxos.retry
```


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_interface.py b/lib/ansible/modules/network/nxos/nxos_interface.py
--- a/lib/ansible/modules/network/nxos/nxos_interface.py
+++ b/lib/ansible/modules/network/nxos/nxos_interface.py
@@ -141,7 +141,7 @@
     sample: ["interface port-channel101", "shutdown"]
 '''
 
-from ansible.module_utils.nxos import get_config, load_config, run_commands
+from ansible.module_utils.nxos import load_config, run_commands
 from ansible.module_utils.nxos import nxos_argument_spec, check_args
 from ansible.module_utils.basic import AnsibleModule
 
@@ -280,7 +280,7 @@ def get_interface(intf, module):
 
     if body:
         interface_table = body['TABLE_interface']['ROW_interface']
-        if interface_table['eth_mode'] == 'fex-fabric':
+        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']:
@@ -700,8 +700,6 @@ def main():
                     cmds.extend(cmds2)
                 end_state, is_default = smart_existing(module, intf_type,
                                                        normalized_interface)
-            else:
-                end_state = get_interfaces_dict(module)[interface_type]
             cmds = [cmd for cmd in cmds if cmd != 'configure']
 
     results['commands'] = cmds


Test patch:
diff --git a/test/units/modules/network/nxos/test_nxos_interface.py b/test/units/modules/network/nxos/test_nxos_interface.py
--- a/test/units/modules/network/nxos/test_nxos_interface.py
+++ b/test/units/modules/network/nxos/test_nxos_interface.py
@@ -37,13 +37,9 @@ def setUp(self):
         self.mock_load_config = patch('ansible.modules.network.nxos.nxos_interface.load_config')
         self.load_config = self.mock_load_config.start()
 
-        self.mock_get_config = patch('ansible.modules.network.nxos.nxos_interface.get_config')
-        self.get_config = self.mock_get_config.start()
-
     def tearDown(self):
         self.mock_run_commands.stop()
         self.mock_load_config.stop()
-        self.mock_get_config.stop()
 
     def load_fixtures(self, commands=None, device=''):
         self.load_config.return_value = None