diff --git a/lib/ansible/module_common.py b/lib/ansible/module_common.py index 090dc941ba8..c97521a4bc7 100644 --- a/lib/ansible/module_common.py +++ b/lib/ansible/module_common.py @@ -807,9 +807,15 @@ class AnsibleModule(object): self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) return backupdest + def cleanup(tmpfile): + if os.path.exists(tmpfile): + try: + os.unlink(tmpfile) + except OSError, e: + sys.stderr.write("could not cleanup %s: %s" % (tmpfile, e)) + def atomic_move(self, src, dest): '''atomically move src to dest, copying attributes from dest, returns true on success''' - rc = False context = None if os.path.exists(dest): try: @@ -829,13 +835,6 @@ class AnsibleModule(object): dest_file = os.path.basename(dest) tmp_dest = "%s/.%s.%s.%s" % (dest_dir,dest_file,os.getpid(),time.time()) - def cleanup(): - if os.path.exists(tmp_dest): - try: - os.unlink(tmp_dest) - except OSError, e: - sys.stderr.write("could not cleanup %s: %s" % (tmp_dest, e)) - try: # leaves tmp file behind when sudo and not root if os.getenv("SUDO_USER") and os.getuid() != 0: # cleanup will happen by 'rm' of tempdir @@ -848,11 +847,9 @@ class AnsibleModule(object): if self.selinux_enabled(): # rename might not preserve context self.set_context_if_different(dest, context, False) - rc = True except (shutil.Error, OSError, IOError), e: - cleanup() + cleanup(tmp_dest) self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, e)) - return rc def run_command(self, args, check_rc=False, close_fds=False, executable=None, data=None): ''' diff --git a/library/files/copy b/library/files/copy index 26c05f7d7cf..c1da485751c 100644 --- a/library/files/copy +++ b/library/files/copy @@ -147,11 +147,7 @@ def main(): (rc,out,err) = module.run_command(validate % src) if rc != 0: module.fail_json(msg="failed to validate: rc:%s error:%s" % (rc,err)) - 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)) + module.atomic_move(src, dest): except IOError: module.fail_json(msg="failed to copy: %s to %s" % (src, dest)) changed = True