Repository: ansible/ansible

Issue:
junos_command: Does not output parsed 'XML' in response
<!---
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
 - Bug / Feature depending on how you look at it ;)

##### COMPONENT NAME
`junos_command`, potentially other `junos_` core modules

##### ANSIBLE VERSION
```
ansible 2.3.1.0
  config file = ansible.cfg
  configured module search path = [u'./plugins/module']
  python version = 2.7.13 (default, Jun 29 2017, 16:19:32) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]
```
##### SUMMARY
Juniper state in a number of places (e.g. https://forums.juniper.net/t5/Automation-Programmability/One-step-further-with-Ansible-to-automate-Junos-devices/ba-p/295120) that the `junos_command` core module is:

> able to natively convert XML to JSON to pass returned information to Ansible

...

> The conversion from XML to JSON is preferred over native JSON output because it works with more version of Junos

Based on the current code on the `devel` branch, this is not the case.

The `junos_command` module *does* go through the effort of parsing the returned XML into a data structure if `display: xml` is specified in the command, [here](/ansible/ansible/blob/devel/lib/ansible/modules/network/junos/junos_command.py#L359)

```python
    try:
        transformed.append(jxmlease.parse(resp))
    except:
        raise ValueError(resp)
```

 However - this is only used to execute conditional checks on the data itself and is discarded, rather than placed into the modules result.

##### STEPS / RESULTS / EXPECTED
https://gist.github.com/benagricola/f3a094625ae4d41f6517016def6c9edd

Rather than requiring extra steps to decode the returned XML manually (e.g. creating a custom filter which itself uses jxmlease to parse the returned XML), it should be possible to pass the existing `transformed` variable into the result hash for direct use (note the additional `output` key in the result, containing the parsed XML structure).


Discussion:
cc @Qalthos @ganeshrn
[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/junos/junos_command.py b/lib/ansible/modules/network/junos/junos_command.py
--- a/lib/ansible/modules/network/junos/junos_command.py
+++ b/lib/ansible/modules/network/junos/junos_command.py
@@ -157,6 +157,11 @@
   returned: always apart from low level errors (such as action plugin)
   type: list
   sample: [['...', '...'], ['...'], ['...']]
+output:
+  description: The set of transformed xml to json format from the commands responses
+  returned: If the I(display) is in C(xml) format.
+  type: list
+  sample: ['...', '...']
 failed_conditions:
   description: The list of conditionals that have failed
   returned: failed
@@ -348,7 +353,7 @@ def main():
         responses = rpc(module, items)
 
         transformed = list()
-
+        output = list()
         for item, resp in zip(items, responses):
             if item['xattrs']['format'] == 'xml':
                 if not HAS_JXMLEASE:
@@ -356,7 +361,9 @@ def main():
                                          'It can be installed using `pip install jxmlease`')
 
                 try:
-                    transformed.append(jxmlease.parse(resp))
+                    json_resp = jxmlease.parse(resp)
+                    transformed.append(json_resp)
+                    output.append(json_resp)
                 except:
                     raise ValueError(resp)
             else:
@@ -390,6 +397,9 @@ def main():
         'stdout_lines': to_lines(responses)
     }
 
+    if output:
+        result['output'] = output
+
     module.exit_json(**result)
 
 if __name__ == '__main__':


Test patch: