Merge pull request #40 from Akasurde/auth_fix

authorized_key: Handle OSError raised

Reviewed-by: https://github.com/apps/ansible-zuul
This commit is contained in:
ansible-zuul[bot] 2020-06-17 21:35:43 +00:00 committed by GitHub
commit 53714a8bc9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -326,7 +326,10 @@ def keyfile(module, user, write=False, path=None, manage_dir=True, follow=False)
if manage_dir: if manage_dir:
if not os.path.exists(sshdir): if not os.path.exists(sshdir):
os.mkdir(sshdir, int('0700', 8)) try:
os.mkdir(sshdir, int('0700', 8))
except OSError as e:
module.fail_json(msg="Failed to create directory %s : %s" % (sshdir, to_native(e)))
if module.selinux_enabled(): if module.selinux_enabled():
module.set_default_selinux_context(sshdir, False) module.set_default_selinux_context(sshdir, False)
os.chown(sshdir, uid, gid) os.chown(sshdir, uid, gid)
@ -462,15 +465,14 @@ def parsekeys(module, lines):
def writefile(module, filename, content): def writefile(module, filename, content):
dummy, tmp_path = tempfile.mkstemp()
fd, tmp_path = tempfile.mkstemp('', 'tmp', os.path.dirname(filename))
f = open(tmp_path, "w")
try: try:
f.write(content) with open(tmp_path, "w") as f:
f.write(content)
except IOError as e: except IOError as e:
module.add_cleanup_file(tmp_path)
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e))) module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, to_native(e)))
f.close()
module.atomic_move(tmp_path, filename) module.atomic_move(tmp_path, filename)