Merge pull request #408 from bcoca/fix_known_hosts_24

fixed 2.4 compatibility
This commit is contained in:
Brian Coca 2015-04-20 13:21:52 -04:00
commit 579cc55cb7

View file

@ -128,18 +128,23 @@ def enforce_state(module, params):
module.fail_json(msg="Failed to read %s: %s" % \ module.fail_json(msg="Failed to read %s: %s" % \
(path,str(e))) (path,str(e)))
try: try:
outf=tempfile.NamedTemporaryFile(dir=os.path.dirname(path), outf=tempfile.NamedTemporaryFile(dir=os.path.dirname(path))
delete=False)
if inf is not None: if inf is not None:
for line in inf: for line in inf:
outf.write(line) outf.write(line)
inf.close() inf.close()
outf.write(key) outf.write(key)
outf.close() outf.flush()
module.atomic_move(outf.name,path) module.atomic_move(outf.name,path)
except (IOError,OSError),e: except (IOError,OSError),e:
module.fail_json(msg="Failed to write to file %s: %s" % \ module.fail_json(msg="Failed to write to file %s: %s" % \
(path,str(e))) (path,str(e)))
try:
outf.close()
except:
pass
params['changed'] = True params['changed'] = True
return params return params
@ -162,16 +167,20 @@ def sanity_check(module,host,key,sshkeygen):
#The approach is to write the key to a temporary file, #The approach is to write the key to a temporary file,
#and then attempt to look up the specified host in that file. #and then attempt to look up the specified host in that file.
try: try:
outf=tempfile.NamedTemporaryFile(delete=False) outf=tempfile.NamedTemporaryFile()
outf.write(key) outf.write(key)
outf.close() outf.flush()
except IOError,e: except IOError,e:
module.fail_json(msg="Failed to write to temporary file %s: %s" % \ module.fail_json(msg="Failed to write to temporary file %s: %s" % \
(outf.name,str(e))) (outf.name,str(e)))
rc,stdout,stderr=module.run_command([sshkeygen,'-F',host, rc,stdout,stderr=module.run_command([sshkeygen,'-F',host,
'-f',outf.name], '-f',outf.name],
check_rc=True) check_rc=True)
os.remove(outf.name) try:
outf.close()
except:
pass
if stdout=='': #host not found if stdout=='': #host not found
module.fail_json(msg="Host parameter does not match hashed host field in supplied key") module.fail_json(msg="Host parameter does not match hashed host field in supplied key")