mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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
This commit is contained in:
parent
b441bcb678
commit
5c5806d669
3 changed files with 10 additions and 5 deletions
1
lib/ansible/plugins/cache/__init__.py
vendored
1
lib/ansible/plugins/cache/__init__.py
vendored
|
@ -20,6 +20,7 @@ __metaclass__ = type
|
||||||
from collections import MutableMapping
|
from collections import MutableMapping
|
||||||
|
|
||||||
from ansible import constants as C
|
from ansible import constants as C
|
||||||
|
from ansible.errors import AnsibleError
|
||||||
from ansible.plugins import cache_loader
|
from ansible.plugins import cache_loader
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
2
lib/ansible/plugins/cache/base.py
vendored
2
lib/ansible/plugins/cache/base.py
vendored
|
@ -31,7 +31,7 @@ except ImportError:
|
||||||
|
|
||||||
class BaseCacheModule(with_metaclass(ABCMeta, object)):
|
class BaseCacheModule(with_metaclass(ABCMeta, object)):
|
||||||
|
|
||||||
display = display
|
_display = display
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
|
|
10
lib/ansible/plugins/cache/jsonfile.py
vendored
10
lib/ansible/plugins/cache/jsonfile.py
vendored
|
@ -68,9 +68,10 @@ class CacheModule(BaseCacheModule):
|
||||||
value = json.load(f)
|
value = json.load(f)
|
||||||
self._cache[key] = value
|
self._cache[key] = value
|
||||||
return value
|
return value
|
||||||
except ValueError:
|
except ValueError as e:
|
||||||
self._display.warning("error while trying to write to %s : %s" % (cachefile, str(e)))
|
self._display.warning("error while trying to read %s : %s. Most likely a corrupt file, so erasing and failing." % (cachefile, str(e)))
|
||||||
raise KeyError
|
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:
|
finally:
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
@ -134,7 +135,10 @@ class CacheModule(BaseCacheModule):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def delete(self, key):
|
def delete(self, key):
|
||||||
|
try:
|
||||||
del self._cache[key]
|
del self._cache[key]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
try:
|
try:
|
||||||
os.remove("%s/%s" % (self._cache_dir, key))
|
os.remove("%s/%s" % (self._cache_dir, key))
|
||||||
except (OSError,IOError) as e:
|
except (OSError,IOError) as e:
|
||||||
|
|
Loading…
Add table
Reference in a new issue