Repository: ansible/ansible

Issue:
Network modules: Graceful message on BadHostKeyException
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
module_utils/network.py

##### ANSIBLE VERSION
```
nsible-playbook 2.3.0 (devel fe95d71fbd) last updated 2016/11/16 13:37:03 (GMT +100)
  lib/ansible/modules/core: (devel 00e64def78) last updated 2016/11/11 15:39:34 (GMT +100)
  lib/ansible/modules/extras: (devel 09f6af8732) last updated 2016/11/11 15:39:43 (GMT +100)
```

##### CONFIGURATION

##### OS / ENVIRONMENT

##### SUMMARY
The network modules throw a backtrace if they get a `BadHostKeyException`.

This fix needs to go into `devel`, and `stable-2.2`

##### STEPS TO REPRODUCE

##### EXPECTED RESULTS
##### ACTUAL RESULTS

```yaml
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_EGGdsg/ansible_module_iosxr_command.py", line 239, in <module>
    main()
  File "/tmp/ansible_EGGdsg/ansible_module_iosxr_command.py", line 202, in main
    runner.add_command(**cmd)
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/netcli.py", line 147, in add_command
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/network.py", line 117, in cli
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/network.py", line 148, in connect
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/iosxr.py", line 57, in connect
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/shell.py", line 226, in connect
  File "/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/shell.py", line 96, in open
  File "/home/johnb/.local/lib/python2.7/site-packages/paramiko/client.py", line 366, in connect
    raise BadHostKeyException(hostname, server_key, our_server_key)
paramiko.ssh_exception.BadHostKeyException: ('iosxr01', <paramiko.rsakey.RSAKey object at 0x7f618658cbd0>, <paramiko.rsakey.RSAKey object at 0x7f618658ca10>)

fatal: [iosxr01]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "iosxr_command"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_EGGdsg/ansible_module_iosxr_command.py\", line 239, in <module>\n    main()\n  File \"/tmp/ansible_EGGdsg/ansible_module_iosxr_command.py\", line 202, in main\n    runner.add_command(**cmd)\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/netcli.py\", line 147, in add_command\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/network.py\", line 117, in cli\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/network.py\", line 148, in connect\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/iosxr.py\", line 57, in connect\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/shell.py\", line 226, in connect\n  File \"/tmp/ansible_EGGdsg/ansible_modlib.zip/ansible/module_utils/shell.py\", line 96, in open\n  File \"/home/johnb/.local/lib/python2.7/site-packages/paramiko/client.py\", line 366, in connect\n    raise BadHostKeyException(hostname, server_key, our_server_key)\nparamiko.ssh_exception.BadHostKeyException: ('iosxr01', <paramiko.rsakey.RSAKey object at 0x7f618658cbd0>, <paramiko.rsakey.RSAKey object at 0x7f618658ca10>)\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}
```



Discussion:
/cc @privateip @Qalthos 


Resolving patch:
diff --git a/lib/ansible/plugins/connection/paramiko_ssh.py b/lib/ansible/plugins/connection/paramiko_ssh.py
--- a/lib/ansible/plugins/connection/paramiko_ssh.py
+++ b/lib/ansible/plugins/connection/paramiko_ssh.py
@@ -245,6 +245,8 @@ def _connect_uncached(self):
                 port=port,
                 **sock_kwarg
             )
+        except paramiko.ssh_exception.BadHostKeyException as e:
+            raise AnsibleConnectionFailure('host key mismatch for %s' % e.hostname)
         except Exception as e:
             msg = str(e)
             if "PID check failed" in msg:


Test patch: