Repository: ansible/ansible

Issue:
junos doesn't cleanly deal with a lack of junos-eznc
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

module_utils/junos.py
##### ANSIBLE VERSION

```
ansible 2.2.0 (devel 697888965e) last updated 2016/09/16 13:36:43 (GMT +100)
  lib/ansible/modules/core: (devel 725cc699e7) last updated 2016/09/16 15:08:20 (GMT +100)
  lib/ansible/modules/extras: (devel 9b5c64e240) last updated 2016/09/16 13:36:49 (GMT +100)

```
##### CONFIGURATION
##### OS / ENVIRONMENT
##### SUMMARY

Junos modules don't cleanly deal with a lack of `junos-eznc`.

In `junos.py` we set `HAS_PYEZ`, however we don't ever read it.

Also should http://docs.ansible.com/ansible/junos_netconf_module.html list `junos-eznc` as a requirement?
##### STEPS TO REPRODUCE

Ensure junos-eznc isn't installed
Run any junos commands
##### EXPECTED RESULTS
##### ACTUAL RESULTS

```
TASK [test_junos_config : setup] ***********************************************
task path: /home/johnb/git/ansible-inc/test-network-modules/roles/test_junos_config/tests/netconf/single.yaml:4
Using module file /home/johnb/git/ansible-inc/ansible/lib/ansible/modules/core/network/junos/junos_config.py
<vsrx01> ESTABLISH LOCAL CONNECTION FOR USER: johnb
<vsrx01> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242 `" && echo ansible-tmp-1474035513.73-204788358334242="` echo $HOME/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242 `" ) && sleep 0'
<vsrx01> PUT /tmp/tmp9vam25 TO /home/johnb/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242/junos_config.py
<vsrx01> EXEC /bin/sh -c 'chmod u+x /home/johnb/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242/ /home/johnb/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242/junos_config.py && sleep 0'
<vsrx01> EXEC /bin/sh -c 'python /home/johnb/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242/junos_config.py; rm -rf "/home/johnb/.ansible/tmp/ansible-tmp-1474035513.73-204788358334242/" > /dev/null 2>&1 && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_vsAILG/ansible_module_junos_config.py", line 292, in <module>
    main()
  File "/tmp/ansible_vsAILG/ansible_module_junos_config.py", line 275, in main
    supports_check_mode=True)
  File "/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/network.py", line 111, in __init__
  File "/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/network.py", line 147, in connect
  File "/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/junos.py", line 96, in connect
NameError: global name 'Device' is not defined

fatal: [vsrx01]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_args": {
            "lines": [
                "set system host-name vsrx01"
            ], 
            "provider": {
                "host": "vsrx01", 
                "password": "Ansible", 
                "transport": "netconf", 
                "username": "ansible"
            }
        }, 
        "module_name": "junos_config"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_vsAILG/ansible_module_junos_config.py\", line 292, in <module>\n    main()\n  File \"/tmp/ansible_vsAILG/ansible_module_junos_config.py\", line 275, in main\n    supports_check_mode=True)\n  File \"/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/network.py\", line 111, in __init__\n  File \"/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/network.py\", line 147, in connect\n  File \"/tmp/ansible_vsAILG/ansible_modlib.zip/ansible/module_utils/junos.py\", line 96, in connect\nNameError: global name 'Device' is not defined\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"



```

If you remove the `try` the actual issue is:

```
ImportError: No module named jnpr.junos\n",
```



Discussion:

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
@@ -72,6 +72,16 @@ def xml_to_string(val):
 class Netconf(object):
 
     def __init__(self):
+        if not HAS_PYEZ:
+            raise NetworkError(
+                msg='junos-eznc >= 1.2.2 is required but does not appear to be installed.  '
+                'It can be installed using `pip install junos-eznc`'
+            )
+        if not HAS_JXMLEASE:
+            raise NetworkError(
+                msg='jxmlease is required but does not appear to be installed.  '
+                'It can be installed using `pip install jxmlease`'
+            )
         self.device = None
         self.config = None
         self._locked = False


Test patch: