Repository: ansible/ansible

Issue:
AnsibleConnectionFailure: Authentication timeout on cli transport

##### ISSUE TYPE
- Bug Report


##### COMPONENT NAME
network_cli

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
2.5.0
```



##### OS / ENVIRONMENT
macOS

##### SUMMARY

Authentication failure with cli transport

```
2017-11-27 12:19:28,866 paramiko.transport kex engine KexGroup14 specified hash_algo <built-in function openssl_sha1>
2017-11-27 12:19:28,868 paramiko.transport Switch to new keys ...
2017-11-27 12:19:28,881 paramiko.transport Trying discovered key xxx in /Users/kedarX/.ssh/id_rsa
2017-11-27 12:19:29,474 paramiko.transport userauth is OK
2017-11-27 12:19:29,757 paramiko.transport Authentication type (publickey) not permitted.
2017-11-27 12:19:29,757 paramiko.transport Allowed methods: [u'password']
2017-11-27 12:19:59,876 p=28933 u=kedarX |  shutdown complete
2017-11-27 12:19:59,963 paramiko.transport EOF in transport thread
```
##### STEPS TO REPRODUCE
Any network task with transport `cli`

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
Successful ssh connection

##### ACTUAL RESULTS
```
    raise AnsibleConnectionFailure(msg)
AnsibleConnectionFailure: Authentication timeout
```

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
Files identified in the description:
* [lib/ansible/plugins/connection/network_cli.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/connection/network_cli.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Qalthos @ganeshrn @gundalow @privateip @rcarrillocruz @trishnaguha
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/plugins/connection/network_cli.py b/lib/ansible/plugins/connection/network_cli.py
--- a/lib/ansible/plugins/connection/network_cli.py
+++ b/lib/ansible/plugins/connection/network_cli.py
@@ -162,7 +162,7 @@ def _connect(self):
             return
 
         p = connection_loader.get('paramiko', self._play_context, '/dev/null')
-        p.set_options(direct={'look_for_keys': bool(self._play_context.password and not self._play_context.private_key_file)})
+        p.set_options(direct={'look_for_keys': not bool(self._play_context.password and not self._play_context.private_key_file)})
         p.force_persistence = self.force_persistence
         ssh = p._connect()
 
@@ -261,6 +261,10 @@ def receive(self, command=None, prompts=None, answer=None):
         while True:
             data = self._ssh_shell.recv(256)
 
+            # when a channel stream is closed, received data will be empty
+            if not data:
+                break
+
             recv.write(data)
             offset = recv.tell() - 256 if recv.tell() > 256 else 0
             recv.seek(offset)


Test patch: