Repository: ansible/ansible

Issue:
nxos_hsrp fails when cli transport is used
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
nxos_hsrp

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  config file = /vagrant/ansible-nxos/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
ubuntu@ubuntu-xenial:/vagrant/ansible-nxos$
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
Ubuntu
NX-OSv 7.3(0)D1(1)

##### SUMMARY
<!--- Explain the problem briefly -->
nxos_hsrp module crashes when cli transport is used. nxapi transport works properly.
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
  vars:
    prov:        
#      transport: nxapi
      transport: cli
#      use_ssl: yes
  
  tasks: 
    - name: Configure HSRP on interfaces
      nxos_hsrp:
        interface: "{{ item.interface }}"
        vip: "{{ item.hsrp_ip }}"
#        auth_string: "{{ item.hsrp_pass }}"
#        auth_type: "{{ item.hsrp_auth_type }}"
        group: "{{ item.hsrp_group }}"
        provider: "{{ prov }}"
      with_items: "{{ interface }}"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
ubuntu@ubuntu-xenial:/vagrant/ansible-nxos$ ansible-playbook -i hosts test1.yml -u cisco -k
SSH password:

PLAY [vlan testing] ************************************************************************************************************************************************************************************************************************************************


TASK [Configure HSRP on interfaces] ********************************************************************************************************************************************************************************************************************************

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: global name 'body' is not defined
failed: [sp2] (item={u'hsrp_auth_type': u'text', u'description': u'Description of int Vlan20', u'interface': u'Vlan20', u'vrf': u'test-vrf', u'hsrp_ip': u'192.168.20.1', u'hsrp_pass': u'cisco', u'hsrp_group': 0}) => {"failed": true, "item": {"description": "De
scription of int Vlan20", "hsrp_auth_type": "text", "hsrp_group": 0, "hsrp_ip": "192.168.20.1", "hsrp_pass": "cisco", "interface": "Vlan20", "vrf": "test-vrf"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_XFjkM1/ansible_module_n
xos_hsrp.py\", line 512, in <module>\n    main()\n  File \"/tmp/ansible_XFjkM1/ansible_module_nxos_hsrp.py\", line 494, in main\n    validate_config(body, vip, module)\nNameError: global name 'body' is not defined\n", "module_stdout": "", "msg": "MODULE FAILUR
E", "rc": 0}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: global name 'body' is not defined
failed: [sp1] (item={u'hsrp_auth_type': u'text', u'description': u'Description of int Vlan20', u'interface': u'Vlan20', u'vrf': u'test-vrf', u'hsrp_ip': u'192.168.20.1', u'hsrp_pass': u'cisco', u'hsrp_group': 0}) => {"failed": true, "item": {"description": "De
scription of int Vlan20", "hsrp_auth_type": "text", "hsrp_group": 0, "hsrp_ip": "192.168.20.1", "hsrp_pass": "cisco", "interface": "Vlan20", "vrf": "test-vrf"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_8c1Zak/ansible_module_n
xos_hsrp.py\", line 512, in <module>\n    main()\n  File \"/tmp/ansible_8c1Zak/ansible_module_nxos_hsrp.py\", line 494, in main\n    validate_config(body, vip, module)\nNameError: global name 'body' is not defined\n", "module_stdout": "", "msg": "MODULE FAILUR
E", "rc": 0}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: global name 'body' is not defined
failed: [sp2] (item={u'hsrp_auth_type': u'text', u'description': u'Description of int Vlan21', u'interface': u'Vlan21', u'vrf': u'test-vrf', u'hsrp_ip': u'192.168.21.1', u'hsrp_pass': u'cisco', u'hsrp_group': 0}) => {"failed": true, "item": {"description": "De
scription of int Vlan21", "hsrp_auth_type": "text", "hsrp_group": 0, "hsrp_ip": "192.168.21.1", "hsrp_pass": "cisco", "interface": "Vlan21", "vrf": "test-vrf"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_ZadBSn/ansible_module_n
xos_hsrp.py\", line 512, in <module>\n    main()\n  File \"/tmp/ansible_ZadBSn/ansible_module_nxos_hsrp.py\", line 494, in main\n    validate_config(body, vip, module)\nNameError: global name 'body' is not defined\n", "module_stdout": "", "msg": "MODULE FAILUR
E", "rc": 0}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: NameError: global name 'body' is not defined
failed: [sp1] (item={u'hsrp_auth_type': u'text', u'description': u'Description of int Vlan21', u'interface': u'Vlan21', u'vrf': u'test-vrf', u'hsrp_ip': u'192.168.21.1', u'hsrp_pass': u'cisco', u'hsrp_group': 0}) => {"failed": true, "item": {"description": "De
scription of int Vlan21", "hsrp_auth_type": "text", "hsrp_group": 0, "hsrp_ip": "192.168.21.1", "hsrp_pass": "cisco", "interface": "Vlan21", "vrf": "test-vrf"}, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_6FviAs/ansible_module_n
xos_hsrp.py\", line 512, in <module>\n    main()\n  File \"/tmp/ansible_6FviAs/ansible_module_nxos_hsrp.py\", line 494, in main\n    validate_config(body, vip, module)\nNameError: global name 'body' is not defined\n", "module_stdout": "", "msg": "MODULE FAILUR
E", "rc": 0}
        to retry, use: --limit @/vagrant/ansible-nxos/test1.retry

PLAY RECAP *********************************************************************************************************************************************************************************************************************************************************

sp1                        : ok=0    changed=0    unreachable=0    failed=1
sp2                        : ok=0    changed=0    unreachable=0    failed=1
```



Discussion:
cc @jedelman8 @GGabriele @rahushen @privateip @rcarrillocruz
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
cc @mikewiebe
[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
@@ -491,6 +491,7 @@ def main():
         else:
             load_config(module, commands)
             if transport == 'cli':
+                body = run_commands(module, commands)
                 validate_config(body, vip, module)
             changed = True
             end_state = get_hsrp_group(group, interface, module)


Test patch: