Repository: ansible/ansible

Issue:
module_utils/junos.py does not handle library jxmlease not being available
##### ISSUE TYPE
- Bug Report
##### OS / ENVIRONMENT

Affects the 'junos' modules.
##### SUMMARY

This is a move of https://github.com/ansible/ansible-modules-core/issues/3614 

module_utils/junos.py  has an import guard around 'import jxmlease' and sets HAS_JXMLEASE, but nothing checks that variable. Any of the modules that
use 

```
from ansible.module_utils.junos import *
```

potentially throw errors if jxmlease is not available.

Cut and paste of the original issue follows:
When junos_command run it through an exception if the library jxmlease is not present
it would be good to check it and print a proper error message

```
TASK [Gather em0 interface information] ****************************************
fatal: [qfx]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jnpr/junos/device.py:652: 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 \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_module_junos_command.py\", line 261, in <module>\n    main()\n  File \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_module_junos_command.py\", line 234, in main\n    response[index] = xml_to_json(response[index])\n  File \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_modlib.zip/ansible/module_utils/junos.py\", line 76, in xml_to_json\nNameError: global name 'jxmlease' is not defined\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
```
##### STEPS TO REPRODUCE

``` yaml

---
- name: Gather em0 interface information
  hosts: all
  connection: local
  gather_facts: no
  tasks:
  - name: Gather em0 interface information
    junos_command:
      host: "{{junos_host}}"
      port: "{{junos_port}}"
      commands:
        - show interfaces em0
      waitfor:
        - "result[1].interface-information.physical-interface.name eq em0"
```
##### ACTUAL RESULTS

```
TASK [Gather em0 interface information] ****************************************
fatal: [qfx]: FAILED! => {"changed": false, "failed": true, "module_stderr": "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jnpr/junos/device.py:652: 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 \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_module_junos_command.py\", line 261, in <module>\n    main()\n  File \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_module_junos_command.py\", line 234, in main\n    response[index] = xml_to_json(response[index])\n  File \"/var/folders/d_/lqhsgxh9313bd3c8r4zf2s48000p48/T/ansible_eZlsBP/ansible_modlib.zip/ansible/module_utils/junos.py\", line 76, in xml_to_json\nNameError: global name 'jxmlease' is not defined\n", "module_stdout": "", "msg": "MODULE FAILURE", "parsed": false}
```



Discussion:
Orig submitted by @dgarros , so mentioning.


Resolving patch:
diff --git a/lib/ansible/module_utils/junos.py b/lib/ansible/module_utils/junos.py
--- a/lib/ansible/module_utils/junos.py
+++ b/lib/ansible/module_utils/junos.py
@@ -350,6 +350,8 @@ def get_module(**kwargs):
         module.fail_json(msg='paramiko is required but does not appear to be installed')
     elif module.params['transport'] == 'netconf' and not HAS_PYEZ:
         module.fail_json(msg='junos-eznc >= 1.2.2 is required but does not appear to be installed')
+    elif module.params['transport'] == 'netconf' and not HAS_JXMLEASE:
+        module.fail_json(msg='jxmlease is required but does not appear to be installed')
 
     module.connect()
     return module


Test patch: