diff --git a/lib/ansible/plugins/cache/__init__.py b/lib/ansible/plugins/cache/__init__.py index b1aecde99a6..2a129e922a0 100644 --- a/lib/ansible/plugins/cache/__init__.py +++ b/lib/ansible/plugins/cache/__init__.py @@ -33,24 +33,17 @@ except ImportError: class FactCache(MutableMapping): def __init__(self, *args, **kwargs): + self._plugin = cache_loader.get(C.CACHE_PLUGIN) if not self._plugin: - raise AnsibleError('Unable to load the facts cache plugin (%s)\n' - 'Check fact cache config options :\n' - 'Current values:\n' - ' fact_caching: %s\n' - ' fact_caching_connection: %s' % - (C.CACHE_PLUGIN, C.CACHE_PLUGIN, C.CACHE_PLUGIN_CONNECTION)) + raise AnsibleError('Unable to load the facts cache plugin (%s).' % (C.CACHE_PLUGIN)) # Backwards compat: self._display isn't really needed, just import the global display and use that. self._display = display - if self._plugin is None: - display.warning("Failed to load fact cache plugins") - return def __getitem__(self, key): - if key not in self: + if not self._plugin.contains(key): raise KeyError return self._plugin.get(key) diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 374065eed37..b3369af2caf 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -43,6 +43,7 @@ from ansible.template import Templar from ansible.utils.listify import listify_lookup_plugin_terms from ansible.utils.vars import combine_vars from ansible.vars.unsafe_proxy import wrap_var +from ansible.module_utils._text import to_native try: from __main__ import display @@ -95,7 +96,6 @@ class VariableManager: def __init__(self): - self._fact_cache = FactCache() self._nonpersistent_fact_cache = defaultdict(dict) self._vars_cache = defaultdict(dict) self._extra_vars = defaultdict(dict) @@ -106,6 +106,14 @@ class VariableManager: self._omit_token = '__omit_place_holder__%s' % sha1(os.urandom(64)).hexdigest() self._options_vars = defaultdict(dict) + # bad cache plugin is not fatal error + try: + self._fact_cache = FactCache() + except AnsibleError as e: + display.warning(to_native(e)) + # fallback to a dict as in memory cache + self._fact_cache = {} + def __getstate__(self): data = dict( fact_cache = self._fact_cache,