Repository: ansible/ansible

Issue:
nxos_hsrp fails for {{ cli }} provider
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
nxos_hsrp

##### ANSIBLE VERSION
```
ansible 2.4.0 (devel 54cd641df4) last updated 2017/07/19 18:06:44 (GMT -400)
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible
  executable location = /root/p2/Projects/nxos_ansible/p1_ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### OS / ENVIRONMENT
Ansible Server : Ubuntu 14.04
Device: Nexus9000 C9396PX running 7.0(3)I7(1)

##### SUMMARY
When we run nxos_hsrp playbook, we get errors when the provider is {{ cli }}. From the looks of the error message, it is as though the feature hsrp is NOT enabled, but on the console, it is verified that the feature hsrp is indeed enabled but still the error comes. And using {{ nxapi }} as provider with the same playbook, there are no errors and all tests pass perfectly. So this is CLI specific issue.

##### STEPS TO REPRODUCE
playbook:
```
- hosts: nxos
  connection: local
  gather_facts: no

  tasks:
  - name: "Enable feature hsrp"
    nxos_feature: 
      feature: hsrp
      state: enabled
      provider: "{{ cli }}"

  - name: "change interface mode"
    nxos_config:
      commands:
        - no switchport
      parents:
        - interface Ethernet1/1
      match: none
      provider: "{{ cli }}"

  - name: "configure nxos_hsrp"
    nxos_hsrp:
      group: 10
      version: 2
      vip: 10.1.1.1
      priority: 150
      interface: Ethernet1/1
      preempt: enabled
      auth_type: text
      auth_string: CISCO
      provider: "{{ cli }}"

```


##### EXPECTED RESULTS
```
PLAY RECAP ********************************************************************************************************************************************
n9k.example.com          : ok=33   changed=10   unreachable=0    failed=0
```

##### ACTUAL RESULTS
```
TASK [nxos_hsrp : configure nxos_hsrp] ****************************************************************************************************************
task path: /root/p2/Projects/nxos_ansible/p1_ansible/test/integration/targets/nxos_hsrp/tests/cli/sanity.yaml:23                                       
<n9k.example.com> connection transport is cli                                                                                                        
<n9k.example.com> using connection plugin network_cli                                                                                                
<n9k.example.com> socket_path: /root/.ansible/pc/66f95cfd2b                                                                                          
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/basic.py                                                    
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/nxos.py                                                     
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/_text.py                                                    
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/parsing/convert_bool.py                                     
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/parsing/__init__.py                                         
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/pycompat24.py                                               
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/six/__init__.py                                             
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/six/_six.py                                                 
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/connection.py                                               
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/network_common.py                                           
Using module_utils file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/module_utils/urls.py                                                     
Using module file /root/p2/Projects/nxos_ansible/p1_ansible/lib/ansible/modules/network/nxos/nxos_hsrp.py                                              
<n9k.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root                                                                                          
<n9k.example.com> EXEC /bin/sh -c 'echo ~ && sleep 0'                                                                                                
<n9k.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356 `" && echo ansible-tmp-1500583835.49-174808425640356="` echo /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356 `" ) && sleep 0'                                   
<n9k.example.com> PUT /tmp/tmpfw_1a5 TO /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356/nxos_hsrp.py                                    
<n9k.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356/ /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356/nxos_hsrp.py && sleep 0'                                                                                                            
<n9k.example.com> EXEC /bin/sh -c '/root/p2/bin/python /root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356/nxos_hsrp.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1500583835.49-174808425640356/" > /dev/null 2>&1 && sleep 0'                                                                       
fatal: [n9k.example.com]: FAILED! => {                                                                                                               
    "changed": false,                                                                                                                                  
    "failed": true,                                                                                                                                    
    "invocation": {                                                                                                                                    
        "module_args": {                                                                                                                               
            "auth_string": "CISCO",                                                                                                                    
            "auth_type": "text",                                                                                                                       
            "config": null,                                                                                                                            
            "group": "10",                                                                                                                             
            "host": null,                                                                                                                              
            "include_defaults": "True",                                                                                                                
            "interface": "Ethernet1/1",                                                                                                                
            "password": null,                                                                                                                          
            "port": null,                                                                                                                              
            "preempt": "enabled",                                                                                                                      
            "priority": "150",                                                                                                                         
            "provider": null,                                                                                                                          
            "save": false,                                                                                                                             
            "ssh_keyfile": null,                                                                                                                       
            "state": "present",                                                                                                                        
            "timeout": 10,                                                                                                                             
            "transport": "cli",                                                                                                                        
            "use_ssl": null,                                                                                                                           
            "username": null,                                                                                                                          
            "validate_certs": null,                                                                                                                    
            "version": "2",                                                                                                                            
            "vip": "10.1.1.1"                                                                                                                          
        }                                                                                                                                              
    },                                                                                                                                                 
    "msg": "interface ethernet1/1\r\r\n                        ^\r\n% Invalid command at '^' marker.\r\n\rn9k.example.com# "                         
}
```



Discussion:
cc @GGabriele @jedelman8 @mikewiebe @privateip @rahushen @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/network/nxos/nxos_hsrp.py b/lib/ansible/modules/network/nxos/nxos_hsrp.py
--- a/lib/ansible/modules/network/nxos/nxos_hsrp.py
+++ b/lib/ansible/modules/network/nxos/nxos_hsrp.py
@@ -374,10 +374,7 @@ def main():
         vip=dict(type='str', required=False),
         auth_type=dict(choices=['text', 'md5'], required=False),
         auth_string=dict(type='str', required=False),
-        state=dict(choices=['absent', 'present'], required=False, default='present'),
-        include_defaults=dict(default=True),
-        config=dict(),
-        save=dict(type='bool', default=False)
+        state=dict(choices=['absent', 'present'], required=False, default='present')
     )
 
     argument_spec.update(nxos_argument_spec)
@@ -464,11 +461,15 @@ def main():
             module.exit_json(**results)
         else:
             load_config(module, commands)
+
+            # validate IP
             if transport == 'cli':
+                commands.insert(0, 'config t')
                 body = run_commands(module, commands)
                 validate_config(body, vip, module)
+
             results['changed'] = True
-            end_state = get_hsrp_group(group, interface, module)
+
             if 'configure' in commands:
                 commands.pop(0)
 


Test patch:
diff --git a/test/units/modules/network/nxos/test_nxos_hsrp.py b/test/units/modules/network/nxos/test_nxos_hsrp.py
--- a/test/units/modules/network/nxos/test_nxos_hsrp.py
+++ b/test/units/modules/network/nxos/test_nxos_hsrp.py
@@ -46,15 +46,16 @@ def load_fixtures(self, commands=None, device=''):
 
     def test_nxos_hsrp(self):
         set_module_args(dict(group='10',
-                             vip='192.0.2.2',
+                             vip='192.0.2.2/8',
                              priority='150',
                              interface='Ethernet1/2',
                              preempt='enabled',
                              host='192.0.2.1'))
         result = self.execute_module(changed=True)
-        self.assertEqual(sorted(result['commands']), sorted(['interface ethernet1/2',
+        self.assertEqual(sorted(result['commands']), sorted(['config t',
+                                                             'interface ethernet1/2',
                                                              'hsrp version 2',
                                                              'hsrp 10',
                                                              'priority 150',
-                                                             'ip 192.0.2.2',
+                                                             'ip 192.0.2.2/8',
                                                              'preempt']))