From 5c5806d669bef10a58b219cc6c2f65f9c35902bd Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Mon, 12 Oct 2015 13:03:49 -0400 Subject: [PATCH] Fixing bugs related to jfonfile cache plugin * corrupt/invalid file causes tracebacks * incorrect initialization of display/_display in BaseCacheModule class * tweaking the way errors in get() on jsonfile caches work, to raise a proper AnsibleError in that situation so the playbook/task is stopped Fixes #12708 --- lib/ansible/plugins/cache/__init__.py | 1 + lib/ansible/plugins/cache/base.py | 2 +- lib/ansible/plugins/cache/jsonfile.py | 12 ++++++++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index 875837d0337..d4a605c08f8 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -20,6 +20,7 @@ __metaclass__ = type from collections import MutableMapping from ansible import constants as C +from ansible.errors import AnsibleError from ansible.plugins import cache_loader try: diff --git a/lib/ansible/plugins/cache/base.py b/lib/ansible/plugins/cache/base.py index b3b6ece9002..84a01829d72 100644 --- a/lib/ansible/plugins/cache/base.py +++ b/lib/ansible/plugins/cache/base.py @@ -31,7 +31,7 @@ except ImportError: class BaseCacheModule(with_metaclass(ABCMeta, object)): - display = display + _display = display @abstractmethod def get(self, key): diff --git a/lib/ansible/plugins/cache/jsonfile.py b/lib/ansible/plugins/cache/jsonfile.py index 3fc3458fb91..8771fc97427 100644 --- a/lib/ansible/plugins/cache/jsonfile.py +++ b/lib/ansible/plugins/cache/jsonfile.py @@ -68,9 +68,10 @@ class CacheModule(BaseCacheModule): value = json.load(f) self._cache[key] = value return value - except ValueError: - self._display.warning("error while trying to write to %s : %s" % (cachefile, str(e))) - raise KeyError + except ValueError as e: + self._display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, str(e))) + self.delete(key) + raise AnsibleError("The JSON cache file %s was corrupt, or did not otherwise contain valid JSON data. It has been removed, so you can re-run your command now.") finally: f.close() @@ -134,7 +135,10 @@ class CacheModule(BaseCacheModule): pass def delete(self, key): - del self._cache[key] + try: + del self._cache[key] + except KeyError: + pass try: os.remove("%s/%s" % (self._cache_dir, key)) except (OSError,IOError) as e: