Repository: ansible/ansible-modules-core

Issue:
junos_command errors out with "TypeError: Type 'str' cannot be serialized"
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### COMPONENT NAME

<!--- Name of the plugin/module/task -->

junos_command core module
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
$ ansible --version
ansible 2.1.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides

```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

No changes to configuration
##### 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.
-->

$ uname -a
Linux dev-net-01 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
##### SUMMARY

<!--- Explain the problem briefly -->

I have an Ansible script where i am simply using junos_command module to get users list from Juniper switch, below is the snippet of my code. I keep getting the RuntimeWarning and TypeError: type 'str' cannot be serialized, whenever i try to run this. Moreover I have been successfully able to run commands like 'show version' using the below code itself. But just not 'show configuration system login' command. Please look into this.

**Script:**

name: / GET USERS / Get list of all the current users on switch
action: junos_command
args: { commands: 'show configuration system login',
          provider: "{{ netconf }}" }
register: curr_users_on_switch

**Error:**

TASK [/ GET USERS / Get list of all the current users on switch] ***************
fatal: [rlab-er1]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/home/mbhadoria/.local/lib/python2.7/site-packages/jnpr/junos/device.py:429: RuntimeWarning: CLI command is for debug use only!
\n  warnings.warn(\"CLI command is for debug use only!\", RuntimeWarning)\nTraceback (most recent call last):
\n  File \"/tmp/ansible_lVOmPp/ansible_module_junos_command.py\", line 261, in <module>
\n    main()
\n  File \"/tmp/ansible_lVOmPp/ansible_module_junos_command.py\", line 233, in main
\n    xmlout.append(xml_to_string(response[index]))
\n  File \"/tmp/ansible_lVOmPp/ansible_modlib.zip/ansible/module_utils/junos.py\", line 79, in xml_to_string\n  File \"src/lxml/lxml.etree.pyx\", line 3350, in lxml.etree.tostring (src/lxml/lxml.etree.c:84534)\nTypeError: Type 'str' cannot be serialized.
\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

Mentioned in above section

<!--- Paste example playbooks or commands between quotes below -->

```
name: / GET USERS / Get list of all the current users on switch
action: junos_command
args: { commands: 'show configuration system login',
          provider: "{{ netconf }}" }
register: curr_users_on_switch
```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

returns the list of users on juniper switch. no error should be expected.
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->

```
TASK [/ GET USERS / Get list of all the current users on switch] ***************
<rlab-er1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1472681123.92-107492843053729 `" && echo ansible-tmp-1472681123.92-107492843053729="` echo $HOME/.ansible/tmp/ansible-tmp-1472681123.92-107492843053729 `" ) && sleep 0'
<rlab-er1> PUT /tmp/tmpU9G6IE TO /home/mbhadoria/.ansible/tmp/ansible-tmp-1472681123.92-107492843053729/junos_command
<rlab-er1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /home/mbhadoria/.ansible/tmp/ansible-tmp-1472681123.92-107492843053729/junos_command; rm -rf "/home/mbhadoria/.ansible/tmp/ansible-tmp-1472681123.92-107492843053729/" > /dev/null 2>&1 && sleep 0'
fatal: [rlab-er1]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "junos_command"}, "module_stderr": "/home/mbhadoria/.local/lib/python2.7/site-packages/jnpr/junos/device.py:429: RuntimeWarning: CLI command is for debug use only!\n  warnings.warn(\"CLI command is for debug use only!\", RuntimeWarning)\nTraceback (most recent call last):\n  File \"/tmp/ansible_mdpif7/ansible_module_junos_command.py\", line 261, in <module>\n    main()\n  File \"/tmp/ansible_mdpif7/ansible_module_junos_command.py\", line 233, in main\n    xmlout.append(xml_to_string(response[index]))\n  File \"/tmp/ansible_mdpif7/ansible_modlib.zip/ansible/module_utils/junos.py\", line 79, in xml_to_string\n  File \"src/lxml/lxml.etree.pyx\", line 3350, in lxml.etree.tostring (src/lxml/lxml.etree.c:84534)\nTypeError: Type 'str' cannot be serialized.\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}

```



Discussion:
<!-- @manubhadoria: Thanks for filing this bug report! -->

@privateip, @gundalow, ping. This issue is waiting on your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

We believe this is fixed, @privateip to test to confirm and close ticket if so


Resolving patch:
diff --git a/network/junos/junos_command.py b/network/junos/junos_command.py
--- a/network/junos/junos_command.py
+++ b/network/junos/junos_command.py
@@ -197,6 +197,10 @@ def parse(module, command_type):
 
         item['command_type'] = command_type
 
+        # show configuration [options] will return as text
+        if item['command'].startswith('show configuration'):
+            item['output'] = 'text'
+
         parsed.append(item)
 
     return parsed


Test patch: