Skip to content

Instantly share code, notes, and snippets.

@daBONDi
Created December 14, 2018 23:25
Show Gist options
  • Save daBONDi/c4bdec8b30f3429a1fe6466a150762d9 to your computer and use it in GitHub Desktop.
Save daBONDi/c4bdec8b30f3429a1fe6466a150762d9 to your computer and use it in GitHub Desktop.
Inventory Plugin Caching, missing Cache Property so self.cache getting not populated from BaseInventoryPlugin on _read_config_data
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import logging
import json
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
NAME = 'testing'
def __init__(self):
super(InventoryModule, self).__init__()
def __populate(self, data):
for host in data:
self.inventory.add_host(host)
def parse(self, inventory, loader, path, cache=False):
logging.basicConfig(filename='/tmp/ansible_debug.log', level=logging.DEBUG)
super(InventoryModule, self).parse(inventory, loader, path, cache)
config_data = self._read_config_data(path=path)
cache_needs_update = False
cache_key = self.get_cache_key(path)
logging.debug("CacheKey: " + cache_key)
logging.debug("config:" + json.dumps(config_data))
logging.debug("Cache:" + str(cache))
if cache:
try:
# ERROR: self.cache = NOTHING
# cache_key is defined
# looks like something happening in BaseInventoryPlugin._read_config_data
# so self._options.get('cache') not getting set
cache_object = self.cache.get(cache_key)
logging.debug("We LOAD FROM CACHE!")
except KeyError:
cache_needs_update = True
if not cache or cache_needs_update:
results = [
"MySampleHost"
]
else:
results = cache_object
self.__populate(results)
if cache_needs_update or (not cache and self.get_option('cache')):
self.cache.set(cache_key, results)
plugin: testing
cache: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment