diff --git a/authorized_key b/authorized_key index e56912727ee..0f9f4a6c895 100644 --- a/authorized_key +++ b/authorized_key @@ -162,7 +162,7 @@ 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() - module.atomic_replace(tmp_path, filename) + module.atomic_move(tmp_path, filename) def enforce_state(module, params): """ diff --git a/copy b/copy index 03578dbec34..26c05f7d7cf 100644 --- a/copy +++ b/copy @@ -19,7 +19,6 @@ # along with Ansible. If not, see . import os -import shutil import time DOCUMENTATION = ''' @@ -144,17 +143,15 @@ def main(): if os.path.islink(dest): os.unlink(dest) open(dest, 'w').close() - # TODO:pid + epoch should avoid most collisions, hostname/mac for those using nfs? - # might be an issue with exceeding path length - dest_tmp = "%s.%s.%s.tmp" % (dest,os.getpid(),time.time()) - shutil.copyfile(src, dest_tmp) if validate: - (rc,out,err) = module.run_command(validate % dest_tmp) + (rc,out,err) = module.run_command(validate % src) if rc != 0: module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err)) - module.atomic_replace(dest_tmp, dest) - except shutil.Error: - module.fail_json(msg="failed to copy: %s and %s are the same" % (src, dest)) + if not module.atomic_move(src, dest): + try: + os.unlink(src) # cleanup tmp files on failure + except OSError, e: + sys.stderr.write("failed to clean up tmp file %s: %s\n" % (src, e)) except IOError: module.fail_json(msg="failed to copy: %s to %s" % (src, dest)) changed = True diff --git a/cron b/cron index 73eca78903b..9baa6601185 100644 --- a/cron +++ b/cron @@ -145,15 +145,7 @@ def get_jobs_file(module, user, tmpfile, cron_file): def install_jobs(module, user, tmpfile, cron_file): if cron_file: cron_file = '/etc/cron.d/%s' % cron_file - try: - module.atomic_replace(tmpfile, cron_file) - except (OSError, IOError), e: - return (1, "", str(e)) - except: - return (1, "", str(e)) - else: - return (0, "", "") - + module.atomic_move(tmpfile, cron_file) else: cmd = "crontab %s %s" % (user, tmpfile) return module.run_command(cmd) diff --git a/service b/service index e972c35e81c..be75728b2c3 100644 --- a/service +++ b/service @@ -334,7 +334,7 @@ class Service(object): os.close(TMP_RCCONF) # Replace previous rc.conf. - self.module.atomic_replace(tmp_rcconf_file, self.rcconf_file) + self.module.atomic_move(tmp_rcconf_file, self.rcconf_file) # =========================================== # Subclass: Linux diff --git a/sysctl b/sysctl index c7f3af71287..874453ae04f 100644 --- a/sysctl +++ b/sysctl @@ -110,10 +110,10 @@ def write_sysctl(module, lines, **sysctl_args): module.fail_json(msg="Failed to write to file %s: %s" % (tmp_path, str(e))) f.flush() f.close() - + # replace the real one - module.atomic_replace(tmp_path, sysctl_args['sysctl_file']) - + module.atomic_move(tmp_path, sysctl_args['sysctl_file']) + # end return sysctl_args