From faadb6830899138de2dfcfca3973a898c5ace3a2 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 6 Apr 2015 23:37:32 -0400 Subject: [PATCH] backup_local now only tries to back up exising files, returns '' otherwise --- lib/ansible/module_utils/basic.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index aaaf85e5e05..54a1a9cfff7 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -1297,14 +1297,18 @@ class AnsibleModule(object): def backup_local(self, fn): '''make a date-marked backup of the specified file, return True or False on success or failure''' - # backups named basename-YYYY-MM-DD@HH:MM:SS~ - ext = time.strftime("%Y-%m-%d@%H:%M:%S~", time.localtime(time.time())) - backupdest = '%s.%s' % (fn, ext) - try: - shutil.copy2(fn, backupdest) - except (shutil.Error, IOError), e: - self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) + backupdest = '' + if os.path.exists(fn): + # backups named basename-YYYY-MM-DD@HH:MM:SS~ + ext = time.strftime("%Y-%m-%d@%H:%M:%S~", time.localtime(time.time())) + backupdest = '%s.%s' % (fn, ext) + + try: + shutil.copy2(fn, backupdest) + except (shutil.Error, IOError), e: + self.fail_json(msg='Could not make backup of %s to %s: %s' % (fn, backupdest, e)) + return backupdest def cleanup(self, tmpfile):