From a033ad4d74643e78c8b6a2c73ade83fd5a72840e Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Thu, 25 Oct 2012 11:20:53 -0700 Subject: [PATCH] Use shutil.copyfile so as to preserve file ownership in authorized_keys --- library/authorized_key | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/authorized_key b/library/authorized_key index 15370aa9885..5ba06fc2b2f 100755 --- a/library/authorized_key +++ b/library/authorized_key @@ -125,7 +125,13 @@ def writekeys(module, filename, 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) + try: + shutil.copyfile(tmp_path, filename) + os.unlink(tmp_path) + except IOError, e: + module.fail_json(msg="Failed to copy temp file to %s: %s" % (filename, str(e))) + except OSError, e: + module.fail_json(msg="Failed to remove temp file: %s" % str(e)) def enforce_state(module, params): """