mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #12173 from ansible/pr/10204
Rebase of 10204 - Add host key for ssh url only.
This commit is contained in:
commit
9d193d8fb4
1 changed files with 25 additions and 15 deletions
|
@ -40,6 +40,8 @@ def add_git_host_key(module, url, accept_hostkey=True, create_dir=True):
|
||||||
|
|
||||||
""" idempotently add a git url hostkey """
|
""" idempotently add a git url hostkey """
|
||||||
|
|
||||||
|
if is_ssh_url(url):
|
||||||
|
|
||||||
fqdn = get_fqdn(url)
|
fqdn = get_fqdn(url)
|
||||||
|
|
||||||
if fqdn:
|
if fqdn:
|
||||||
|
@ -52,13 +54,24 @@ def add_git_host_key(module, url, accept_hostkey=True, create_dir=True):
|
||||||
else:
|
else:
|
||||||
module.fail_json(msg="%s has an unknown hostkey. Set accept_hostkey to True or manually add the hostkey prior to running the git module" % fqdn)
|
module.fail_json(msg="%s has an unknown hostkey. Set accept_hostkey to True or manually add the hostkey prior to running the git module" % fqdn)
|
||||||
|
|
||||||
|
def is_ssh_url(url):
|
||||||
|
|
||||||
|
""" check if url is ssh """
|
||||||
|
|
||||||
|
if "@" in url and "://" not in url:
|
||||||
|
return True
|
||||||
|
for scheme in "ssh://", "git+ssh://", "ssh+git://":
|
||||||
|
if url.startswith(scheme):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def get_fqdn(repo_url):
|
def get_fqdn(repo_url):
|
||||||
|
|
||||||
""" chop the hostname out of a giturl """
|
""" chop the hostname out of a url """
|
||||||
|
|
||||||
result = None
|
result = None
|
||||||
if "@" in repo_url and "://" not in repo_url:
|
if "@" in repo_url and "://" not in repo_url:
|
||||||
# most likely a git@ or ssh+git@ type URL
|
# most likely an user@host:path or user@host/path type URL
|
||||||
repo_url = repo_url.split("@", 1)[1]
|
repo_url = repo_url.split("@", 1)[1]
|
||||||
if ":" in repo_url:
|
if ":" in repo_url:
|
||||||
repo_url = repo_url.split(":")[0]
|
repo_url = repo_url.split(":")[0]
|
||||||
|
@ -69,9 +82,6 @@ def get_fqdn(repo_url):
|
||||||
elif "://" in repo_url:
|
elif "://" in repo_url:
|
||||||
# this should be something we can parse with urlparse
|
# this should be something we can parse with urlparse
|
||||||
parts = urlparse.urlparse(repo_url)
|
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
|
|
||||||
# parts[1] will be empty on python2.4 on ssh:// or git:// urls, so
|
# parts[1] will be empty on python2.4 on ssh:// or git:// urls, so
|
||||||
# ensure we actually have a parts[1] before continuing.
|
# ensure we actually have a parts[1] before continuing.
|
||||||
if parts[1] != '':
|
if parts[1] != '':
|
||||||
|
|
Loading…
Reference in a new issue