From 6a08b16c37e67dd9cfb5804593c7ed4c783fa8f5 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Sat, 28 Apr 2018 16:24:18 +0200 Subject: [PATCH] Ensure remove files work when file was already removed If a file disappears when you are removing it, this will ensure it doesn't fail and continues as expected. --- lib/ansible/modules/files/file.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index 0e0823e5a5..a861690bf4 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -281,8 +281,9 @@ def main(): else: try: os.unlink(b_path) - except Exception as e: - module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e)) + except OSError as e: + if e.errno != errno.ENOENT: # It may already have been removed + module.fail_json(path=path, msg="unlinking failed: %s " % to_native(e)) module.exit_json(path=path, changed=True, diff=diff) else: module.exit_json(path=path, changed=False) @@ -415,7 +416,11 @@ def main(): os.rmdir(b_path) elif prev_state == 'directory' and state == 'hard': if os.path.exists(b_path): - os.remove(b_path) + try: + os.unlink(b_path) + except OSError as e: + if e.errno != errno.ENOENT: # It may already have been removed + raise if state == 'hard': os.link(b_src, b_tmppath) else: