mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-01-11 15:15:26 +01:00
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:
commit
53714a8bc9
1 changed files with 8 additions and 6 deletions
|
|
@ -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)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue