mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-01 08:13:08 +00:00
Merge pull request #6762 from jimi-c/issue_6731_fix_host_key_dir_creation
Add option to create host_key directory if it doesn't exist
This commit is contained in:
@@ -30,7 +30,7 @@ import hmac
|
||||
from hashlib import sha1
|
||||
HASHED_KEY_MAGIC = "|1|"
|
||||
|
||||
def add_git_host_key(module, url, accept_hostkey=True):
|
||||
def add_git_host_key(module, url, accept_hostkey=True, create_dir=True):
|
||||
|
||||
""" idempotently add a git url hostkey """
|
||||
|
||||
@@ -40,7 +40,7 @@ def add_git_host_key(module, url, accept_hostkey=True):
|
||||
known_host = check_hostkey(module, fqdn)
|
||||
if not known_host:
|
||||
if accept_hostkey:
|
||||
rc, out, err = add_host_key(module, fqdn)
|
||||
rc, out, err = add_host_key(module, fqdn, create_dir=create_dir)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="failed to add %s hostkey: %s" % (fqdn, out + err))
|
||||
else:
|
||||
@@ -120,7 +120,7 @@ def not_in_host_file(self, host):
|
||||
return True
|
||||
|
||||
|
||||
def add_host_key(module, fqdn, key_type="rsa"):
|
||||
def add_host_key(module, fqdn, key_type="rsa", create_dir=False):
|
||||
|
||||
""" use ssh-keyscan to add the hostkey """
|
||||
|
||||
@@ -136,7 +136,15 @@ def add_host_key(module, fqdn, key_type="rsa"):
|
||||
user_ssh_dir = os.path.expanduser(user_ssh_dir)
|
||||
|
||||
if not os.path.exists(user_ssh_dir):
|
||||
module.fail_json(msg="%s does not exist" % user_ssh_dir)
|
||||
if create_dir:
|
||||
try:
|
||||
os.makedirs(user_ssh_dir, 0700)
|
||||
except:
|
||||
module.fail_json(msg="failed to create host key directory: %s" % user_ssh_dir)
|
||||
else:
|
||||
module.fail_json(msg="%s does not exist" % user_ssh_dir)
|
||||
elif not os.path.isdir(user_ssh_dir):
|
||||
module.fail_json(msg="%s is not a directory" % user_ssh_dir)
|
||||
|
||||
this_cmd = "%s -t %s %s" % (keyscan_cmd, key_type, fqdn)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user