mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-02 06:30:19 -07:00
add inventory caching & use in virtualbox inventory plugin (#34510)
* Inventory caching * Add inventory caching for virtualbox * Don't populate cache for virtualbox with stdout, use a dict of inventory instead * Fix error creating the cache dir if it doesn't exist * Keep cache default False and set to True in VariableManager __init__ * Check all groups before determining if a host is ungrouped.
This commit is contained in:
parent
9fdaa86c9f
commit
4a1cc661c4
5 changed files with 153 additions and 16 deletions
71
lib/ansible/plugins/cache/__init__.py
vendored
71
lib/ansible/plugins/cache/__init__.py
vendored
|
@ -79,12 +79,24 @@ class BaseFileCacheModule(BaseCacheModule):
|
|||
self.plugin_name = self.__module__.split('.')[-1]
|
||||
self._timeout = float(C.CACHE_PLUGIN_TIMEOUT)
|
||||
self._cache = {}
|
||||
self._cache_dir = None
|
||||
self._cache_dir = self._get_cache_connection(C.CACHE_PLUGIN_CONNECTION)
|
||||
self._set_inventory_cache_override(**kwargs)
|
||||
self.validate_cache_connection()
|
||||
|
||||
if C.CACHE_PLUGIN_CONNECTION:
|
||||
# expects a dir path
|
||||
self._cache_dir = os.path.expanduser(os.path.expandvars(C.CACHE_PLUGIN_CONNECTION))
|
||||
def _get_cache_connection(self, source):
|
||||
if source:
|
||||
try:
|
||||
return os.path.expanduser(os.path.expandvars(source))
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
def _set_inventory_cache_override(self, **kwargs):
|
||||
if kwargs.get('cache_timeout'):
|
||||
self._timeout = kwargs.get('cache_timeout')
|
||||
if kwargs.get('cache_connection'):
|
||||
self._cache_dir = self._get_cache_connection(kwargs.get('cache_connection'))
|
||||
|
||||
def validate_cache_connection(self):
|
||||
if not self._cache_dir:
|
||||
raise AnsibleError("error, '%s' cache plugin requires the 'fact_caching_connection' config option "
|
||||
"to be set (to a writeable directory path)" % self.plugin_name)
|
||||
|
@ -141,7 +153,7 @@ class BaseFileCacheModule(BaseCacheModule):
|
|||
def has_expired(self, key):
|
||||
|
||||
if self._timeout == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
cachefile = "%s/%s" % (self._cache_dir, key)
|
||||
try:
|
||||
|
@ -284,3 +296,52 @@ class FactCache(MutableMapping):
|
|||
host_cache = self._plugin.get(key)
|
||||
host_cache.update(value)
|
||||
self._plugin.set(key, host_cache)
|
||||
|
||||
|
||||
class InventoryFileCacheModule(BaseFileCacheModule):
|
||||
"""
|
||||
A caching module backed by file based storage.
|
||||
"""
|
||||
def __init__(self, plugin_name, timeout, cache_dir):
|
||||
|
||||
self.plugin_name = plugin_name
|
||||
self._timeout = timeout
|
||||
self._cache = {}
|
||||
self._cache_dir = self._get_cache_connection(cache_dir)
|
||||
self.validate_cache_connection()
|
||||
self._plugin = self.get_plugin(plugin_name)
|
||||
|
||||
def validate_cache_connection(self):
|
||||
try:
|
||||
super(InventoryFileCacheModule, self).validate_cache_connection()
|
||||
except AnsibleError as e:
|
||||
cache_connection_set = False
|
||||
else:
|
||||
cache_connection_set = True
|
||||
|
||||
if not cache_connection_set:
|
||||
raise AnsibleError("error, '%s' inventory cache plugin requires the one of the following to be set:\n"
|
||||
"ansible.cfg:\n[default]: fact_caching_connection,\n[inventory]: cache_connection;\n"
|
||||
"Environment:\nANSIBLE_INVENTORY_CACHE_CONNECTION,\nANSIBLE_CACHE_PLUGIN_CONNECTION."
|
||||
"to be set to a writeable directory path" % self.plugin_name)
|
||||
|
||||
def get(self, cache_key):
|
||||
|
||||
if not self.contains(cache_key):
|
||||
# Check if cache file exists
|
||||
raise KeyError
|
||||
|
||||
return super(InventoryFileCacheModule, self).get(cache_key)
|
||||
|
||||
def get_plugin(self, plugin_name):
|
||||
plugin = cache_loader.get(plugin_name, cache_connection=self._cache_dir, cache_timeout=self._timeout)
|
||||
if not plugin:
|
||||
raise AnsibleError('Unable to load the facts cache plugin (%s).' % (plugin_name))
|
||||
self._cache = {}
|
||||
return plugin
|
||||
|
||||
def _load(self, path):
|
||||
return self._plugin._load(path)
|
||||
|
||||
def _dump(self, value, path):
|
||||
return self._plugin._dump(value, path)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue