Inventory Plugin cache is not being flushed in Ansible 2.7 anymore

8 views
Skip to first unread message

Philip Douglass

unread,
Nov 22, 2019, 9:59:06 AM11/22/19
to Ansible Development
Due to the following deprecation warnings:

[DEPRECATION WARNING]: InventoryModule should utilize self._cache as a dict instead of self.cache. When expecting a KeyError, use self._cache[key] instead of using self.cache.get(key). self._cache is a dictionary and will return a default value instead of raising a KeyError when the key does not exist. This feature will be removed in version 2.12.

[DEPRECATION WARNING]: InventoryModule should utilize self._cache as a dict instead of self.cache. To set the self._cache dictionary, use self._cache[key] = value instead of self.cache.set(key, value). To force update the underlying cache plugin with the contents of self._cache before parse() is complete, call self.set_cache_plugin and it will use the self._cache dictionary to update the cache plugin. This feature will be removed in version 2.12.

I've updated my custom inventory plugins:


def _fetch(self, url):
    cache_key
= self.get_cache_key(url)

   
if self.attempt_to_read_cache:
       
try:
           
# return self.cache.get(cache_key)  # Old stable-2.7 method
           
return self._cache[cache_key]       # New stable-2.8+ method
       
except KeyError:
            display
.debug("cache miss")
           
self.cache_needs_update = True

    results
= self._process_url(url)

   
if self.cache_needs_update:
       
# self.cache.set(cache_key, results)    # Old stable-2.7 method
       
self._cache[cache_key] = results        # New stable-2.8+ method (not working in stable-2.7!)

   
return results


However, when running this with Ansible 2.7, the cache doesn't get flushed to disk, so each attempt to read the cache results in a cache miss. Is there any way to make this backwards compatible with Ansible 2.7 without causing a DeprecationWarning?
Reply all
Reply to author
Forward
0 new messages