Repository: ansible/ansible

Issue:
nxos_vtp_version : error while getting vtp password for N1 images.
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

When no vtp password is configured, the module errors out trying to get the vtp password.

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
- nxos_vtp_version
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0 (detached HEAD d14467b029) last updated 2017/09/21 15:17: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/agents-ci/ansible/lib/ansible
  executable location = /root/agents-ci/ansible/bin/ansible
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### 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.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Ubuntu 14.04

##### SUMMARY
<!--- Explain the problem briefly -->
On N1 images, when performing 'show vtp password | json' while password is not configured generates a weird message 'output conversion failed due to conv error, bytes 0x90 0x3C 0x2F 0x70'. The module expects a dictionary, but we get a weird string. 


##### 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
  - name: configure vtp version
    nxos_vtp_version: &configure
      version: 2
      provider: "{{ connection }}"
    register: result
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Expected module to not fail.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->



<!--- Paste verbatim command output between quotes below -->
```
TASK [nxos_vtp_version : configure vtp version] ***********************************************************************************************************
task path: /root/agents-ci/ansible/test/integration/targets/nxos_vtp_version/tests/common/sanity.yaml:11
<n5k-xxx.example.com> connection transport is cli
<n5k-xxx.example.com> using connection plugin network_cli
<n5k-xxx.example.com> socket_path: /root/.ansible/pc/1b4e3e6315
Using module file /root/agents-ci/ansible/lib/ansible/modules/network/nxos/nxos_vtp_version.py
<n5k-xxx.example.com> ESTABLISH LOCAL CONNECTION FOR USER: root
<n5k-xxx.example.com> EXEC /bin/sh -c 'echo ~ && sleep 0'
<n5k-xxx.example.com> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998 `" && echo ansible-tmp-1506024091.06-54110608208998="` echo /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998 `" ) && sleep 0'
<n5k-xxx.example.com> PUT /tmp/tmpeJZyoX TO /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py
<n5k-xxx.example.com> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/ /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py && sleep 0'
<n5k-xxx.example.com> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/nxos_vtp_version.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1506024091.06-54110608208998/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 208, in <module>
    main()
  File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 172, in main
    existing = get_vtp_config(module)
  File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 142, in get_vtp_config
    vtp_parsed['vtp_password'] = get_vtp_password(module)
  File "/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py", line 150, in get_vtp_password
    password = body['passwd'] 
TypeError: string indices must be integers, not str
FAILED! => {
    "changed": false, 
    "failed": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 208, in <module>\n    main()\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 172, in main\n    existing = get_vtp_config(module)\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 142, in get_vtp_config\n    vtp_parsed['vtp_password'] = get_vtp_password(module)\n  File \"/tmp/ansible_ovCMww/ansible_module_nxos_vtp_version.py\", line 150, in get_vtp_password\n    password = body['passwd']\nTypeError: string indices must be integers, not str\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE",  
    "rc": 0
}
```



Discussion:
cc @GGabriele @jedelman8 @mikewiebe @privateip @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_vtp_domain.py b/lib/ansible/modules/network/nxos/nxos_vtp_domain.py
--- a/lib/ansible/modules/network/nxos/nxos_vtp_domain.py
+++ b/lib/ansible/modules/network/nxos/nxos_vtp_domain.py
@@ -149,10 +149,13 @@ def get_vtp_config(module):
 def get_vtp_password(module):
     command = 'show vtp password'
     body = execute_show_command(command, module)[0]
-    password = body['passwd']
-    if password:
-        return str(password)
-    else:
+    try:
+        password = body['passwd']
+        if password:
+            return str(password)
+        else:
+            return ""
+    except TypeError:
         return ""
 
 
diff --git a/lib/ansible/modules/network/nxos/nxos_vtp_password.py b/lib/ansible/modules/network/nxos/nxos_vtp_password.py
--- a/lib/ansible/modules/network/nxos/nxos_vtp_password.py
+++ b/lib/ansible/modules/network/nxos/nxos_vtp_password.py
@@ -180,10 +180,13 @@ def get_vtp_config(module):
 def get_vtp_password(module):
     command = 'show vtp password'
     body = execute_show_command(command, module)[0]
-    password = body['passwd']
-    if password:
-        return str(password)
-    else:
+    try:
+        password = body['passwd']
+        if password:
+            return str(password)
+        else:
+            return ""
+    except TypeError:
         return ""
 
 
diff --git a/lib/ansible/modules/network/nxos/nxos_vtp_version.py b/lib/ansible/modules/network/nxos/nxos_vtp_version.py
--- a/lib/ansible/modules/network/nxos/nxos_vtp_version.py
+++ b/lib/ansible/modules/network/nxos/nxos_vtp_version.py
@@ -147,10 +147,13 @@ def get_vtp_config(module):
 def get_vtp_password(module):
     command = 'show vtp password'
     body = execute_show_command(command, module)[0]
-    password = body['passwd']
-    if password:
-        return str(password)
-    else:
+    try:
+        password = body['passwd']
+        if password:
+            return str(password)
+        else:
+            return ""
+    except TypeError:
         return ""
 
 


Test patch: