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
pull/6770/head
James Cammarata 11 years ago
commit 81b430ddce

@ -30,7 +30,7 @@ import hmac
from hashlib import sha1 from hashlib import sha1
HASHED_KEY_MAGIC = "|1|" 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 """ """ 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) known_host = check_hostkey(module, fqdn)
if not known_host: if not known_host:
if accept_hostkey: 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: if rc != 0:
module.fail_json(msg="failed to add %s hostkey: %s" % (fqdn, out + err)) module.fail_json(msg="failed to add %s hostkey: %s" % (fqdn, out + err))
else: else:
@ -120,7 +120,7 @@ def not_in_host_file(self, host):
return True 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 """ """ 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) user_ssh_dir = os.path.expanduser(user_ssh_dir)
if not os.path.exists(user_ssh_dir): if not os.path.exists(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) 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) this_cmd = "%s -t %s %s" % (keyscan_cmd, key_type, fqdn)

Loading…
Cancel
Save