Repository: ansible/ansible

Issue:
nxos_interface module will not reset interfaces to theyre defaults. It returns the interface you are trying to default does not exist
<!---
Using latest branch ansible 2.3.2.0
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Feature Idea


##### COMPONENT NAME
<!--- nxos_interface -->

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
2.3.1.0
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Centos 7.3
-->

##### SUMMARY
<!--- nxos_interface module will not reset interfaces to theyre defaults. It returns the interface you are trying to default does not exist -->

##### STEPS TO REPRODUCE
<!---
  tasks:
   - name: "return interface to its default"
     nxos_interface:
       interface: Ethernet1/47
       state: default
       provider: "{{nexus_auth_provider}}"
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
  tasks:
   - name: "return interface to its default"
     nxos_interface:
       interface: Ethernet1/47
       state: default
       provider: "{{nexus_auth_provider}}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Expect the interface to be set to its default. The interface exists because I can see it on the switch and update it with the interface module when using state: present

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
<!--- Paste verbatim command output between quotes below -->
```
 "msg": "interface you are trying to default does not exist"
```



Discussion:

The problem occurs because the list body = execute_show_command(command, module,command_type='cli_show_ascii')[0]  is always empty - even though the command variable: command = 'show run interface ' + interface  is not empty and the command runs quite happily on the nexus we are configuring - I have included the is_default_interface function from the module below:
                                    

def is_default_interface(interface, module):
    """Checks to see if interface exists and if it is a default config
    Args:
        interface (str): full name of interface, i.e. vlan10,
            Ethernet1/1, loopback10
    Returns:
        True: if interface has default config
        False: if it does not have a default config
        DNE (str): if the interface does not exist - loopbacks, SVIs, etc.
    """
    command = 'show run interface ' + interface

    try:
        body = execute_show_command(command, module,
                                    command_type='cli_show_ascii')[0]
    except IndexError:
        body = ''

    if body:
        raw_list = body.split('\n')
        found = False
        for line in raw_list:
            if line.startswith('interface'):
                found = True
            if found and line and not line.startswith('interface'):
                return False
        return True

    else:
        return 'DNE'

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
@@ -186,8 +186,7 @@ def is_default_interface(interface, module):
     command = 'show run interface ' + interface
 
     try:
-        body = execute_show_command(command, module,
-                                    command_type='cli_show_ascii')[0]
+        body = run_commands(module, [command])[0]
     except IndexError:
         body = ''
 


Test patch: