Repository: ansible/ansible

Issue:
Error in ssh conection plugin when /etc/ssh/ssh_known_hosts file has some lines only with whitespaces
##### Issue Type:

Bug Report
##### Ansible Version:

1.7.2
##### Environment:

“N/A”
##### Summary:

In case of using the 'ssh' transport if the ssh_known_hosts contains one or more lines only composed by whitespaces (" " character). All the connections fail.
##### Steps To Reproduce:

Take an Ansible installation and add a line only with whitespaces at any point of the /etc/ssh/ssh_known_hosts file.
Set transport to "ssh" (or "smart" if you are using an O.S. with new versions of OpenSSH) in ansible.cfg
Then, any module you use will fail. For example:

```
ansible all -m ping
```
##### Expected Results:

```
10.0.0.1 | success >> {
    "changed": false, 
    "ping": "pong"
}
```
##### Actual Results:

```
158.42.104.90 | FAILED => Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 561, in _executor
    exec_rc = self._executor_internal(host, new_stdin)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 666, in _executor_internal
    return self._executor_internal_inner(host, self.module_name, self.module_args, inject, port, complex_args=complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 884, in _executor_internal_inner
    result = handler.run(conn, tmp, module_name, module_args, inject, complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/action_plugins/normal.py", line 57, in run
    return self.runner._execute_module(conn, tmp, module_name, module_args, inject=inject, complex_args=complex_args)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 526, in _execute_module
    res = self._low_level_exec_command(conn, cmd, tmp, sudoable=sudoable, in_data=in_data)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/__init__.py", line 1025, in _low_level_exec_command
    in_data=in_data)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/connection_plugins/ssh.py", line 292, in exec_command
    not_in_host_file = self.not_in_host_file(self.host)
  File "/usr/lib/python2.7/dist-packages/ansible/runner/connection_plugins/ssh.py", line 236, in not_in_host_file
    if tokens[0].find(self.HASHED_KEY_MAGIC) == 0:
IndexError: list index out of range
```

The problem is on line 233 of the file ansible/runner/connection_plugins/ssh.py:

```
if line is None or " " not in line:
```

This comprobation is not correct. It must be someting like that:

```
if not line.strip():
```

As far as I understand the both comprobations: line is None or " " not in line, have no sense in this part of the code.



Discussion:

Resolving patch:
diff --git a/lib/ansible/runner/connection_plugins/ssh.py b/lib/ansible/runner/connection_plugins/ssh.py
--- a/lib/ansible/runner/connection_plugins/ssh.py
+++ b/lib/ansible/runner/connection_plugins/ssh.py
@@ -230,6 +230,7 @@ def not_in_host_file(self, host):
                 host_fh.close()
                 
             for line in data.split("\n"):
+                line = line.strip()
                 if line is None or " " not in line:
                     continue
                 tokens = line.split()


Test patch: