Repository: ansible/ansible

Issue:
git module indicates unknown hostkey when repo has a port
##### Issue Type:

“Bug Report”
##### Ansible Version:

ansible 1.7 (devel 05eb6aacec) last updated 2014/06/26 13:36:36 (GMT +000)
##### Environment:

Centos 5/6
##### Summary:

when using git module, if ssh://git@192.168.1.89:1357/, the host check process breaks.
##### Steps To Reproduce:
1. have a git repo available over ssh on port 22
2. have a working git checkout with ssh://git@fqdn/
3. try with ssh://git@fqdn:22/
##### Expected Results:

It should work as expected, on port 22 and on non-standard ports.
##### Actual Results:

(let me add that the host key IS in known_hosts)

with accept_hostkey = False

failed: [127.0.0.1] => (item=..) => {"failed": true, "item": ".."}
msg: 192.168.1.89:22 has an unknown hostkey. Set accept_hostkey to True or manually add the hostkey prior to running the git module

with accept_hostkey = True

failed: [127.0.0.1] => (item=..) => {"failed": true, "item": ".."}
msg: failed to add 192.168.1.89:22 hostkey: getaddrinfo 192.168.1.89:22: Name or service not known

The problem seems to come from ansiwork/lib/ansible/module_utils/known_hosts.py
get_fqdn does not remove the port.



Discussion:
Can you please provide the line you are using from your playbook?

Here is the task ; not sure it will help as it stands.

```
- name: update master git clones
  git: repo={{ git_giturl }}
       dest={{ git_master }}
       version={{ git_pack.branch }}
       update=yes
  with_items: git_packs
  when: v_packs_hashes is not defined
```

In the code I see

```
        parts = urlparse.urlparse(repo_url)
        if 'ssh' not in parts[0] and 'git' not in parts[0]:
            # don't try and scan a hostname that's not ssh
            return None
        if parts[1] != '':
            result = parts[1]
        if "@" in result:
            result = result.split("@", 1)[1]
```

and from urlparse documentation https://docs.python.org/2/library/urlparse.html :

```
>>> from urlparse import urlparse
>>> urlparse('//www.cwi.nl:80/%7Eguido/Python.html')
ParseResult(scheme='', netloc='www.cwi.nl:80', path='/%7Eguido/Python.html',
           params='', query='', fragment='')

netloc <=> index 1

```

In {{git_giturl}} I have for example

```
ssh://git@192.168.1.89:1357/volume1/homes/git
```

It broke today after I updated to ansible HEAD. It was working with my previous checkout on Jun 10, 2014

it could be that  this commit https://github.com/ansible/ansible/commit/3fb3e7e6dce046bec9d2e8c52756f368d18702c8
changed the codepath for the 'ssh' case from a code path where the port is removed `repo_url = repo_url.split(":")[0]` to a code path where it is not removed.

I'm surprised this worked previously, as the port has always simply been dropped. I'm assuming that system also has SSH running on the standard port 22 as well?

Just to make it clear :

```
repo=ssh://git@192.168.1.89/volume1/homes/git
```

works

```
repo=ssh://git@192.168.1.89:22/volume1/homes/git
```

does not work

which basically makes it a no go for non standard ports.

I also reported this here: https://groups.google.com/forum/#!msg/ansible-project/ah1p2js7D0U/yWuABplQ54gJ but never got the chance to open an issue about it here. Don't think this can be considered issue hijacking as the underlying problem looks to be the same, nor I want to create a duplicate issue.

Cheers.

@jeromew right, my question was does that system have an SSH daemon running on 22, because right now (and before) it always defaulted to 22 since we never specified the port to ssh-keyscan. That was more of a curiosity, not really critical to the issue - I was just surprised this worked before for git daemons not running on :22.

@jimi-c I supposed it worked because :
- the first ssh-keyscan was done when I was on internal LAN with port 22
- and then the entry was found in the known_hosts because the :port was removed for the lookup

or maybe I added the entry in known_hosts by hand.

with the bug, ansible is not finding the entry in known_hosts even for IP:22 ; so at this stage I removed the :22.


Resolving patch:
diff --git a/lib/ansible/module_utils/known_hosts.py b/lib/ansible/module_utils/known_hosts.py
--- a/lib/ansible/module_utils/known_hosts.py
+++ b/lib/ansible/module_utils/known_hosts.py
@@ -74,6 +74,8 @@ def get_fqdn(repo_url):
             return None
         if parts[1] != '':
             result = parts[1]
+            if ":" in result:
+                result = result.split(":")[0]
         if "@" in result:
             result = result.split("@", 1)[1]
 


Test patch: