mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1444 from sfromm/issue1211
Write ssh key to temp file
This commit is contained in:
commit
27c38c357d
1 changed files with 12 additions and 5 deletions
|
@ -68,6 +68,8 @@ import sys
|
|||
import os
|
||||
import pwd
|
||||
import os.path
|
||||
import tempfile
|
||||
import shutil
|
||||
|
||||
def keyfile(user, write=False):
|
||||
"""
|
||||
|
@ -114,11 +116,16 @@ def readkeys(filename):
|
|||
f.close()
|
||||
return keys
|
||||
|
||||
def writekeys( filename, keys):
|
||||
def writekeys(module, filename, keys):
|
||||
|
||||
f = open(filename,"w")
|
||||
f.writelines( (key + "\n" for key in keys) )
|
||||
fd, tmp_path = tempfile.mkstemp('', 'tmp', os.path.dirname(filename))
|
||||
f = open(tmp_path,"w")
|
||||
try:
|
||||
f.writelines( (key + "\n" for key in keys) )
|
||||
except IOError, e:
|
||||
module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e)))
|
||||
f.close()
|
||||
shutil.move(tmp_path, filename)
|
||||
|
||||
def enforce_state(module, params):
|
||||
"""
|
||||
|
@ -139,13 +146,13 @@ def enforce_state(module, params):
|
|||
if present:
|
||||
module.exit_json(changed=False)
|
||||
keys.append(key)
|
||||
writekeys(keyfile(user,write=True), keys)
|
||||
writekeys(module, keyfile(user,write=True), keys)
|
||||
|
||||
elif state=="absent":
|
||||
if not present:
|
||||
module.exit_json(changed=False)
|
||||
keys.remove(key)
|
||||
writekeys(keyfile(user,write=True), keys)
|
||||
writekeys(module, keyfile(user,write=True), keys)
|
||||
|
||||
params['changed'] = True
|
||||
return params
|
||||
|
|
Loading…
Reference in a new issue