[PR #9760/d696bb7b backport][stable-9] proxmox inventory: proposal for #9710 (caching) (#9769)

proxmox inventory: proposal for #9710 (caching) (#9760)

* Proposal for #9710

* Fixed comments

* Fixed trailing whitespace

* Fixed changelog fragment

(cherry picked from commit d696bb7b89)

Co-authored-by: Dirk S. <iqt4@users.noreply.github.com>
pull/9779/head
patchback[bot] 2025-02-17 19:26:01 +01:00 committed by GitHub
parent fb68abd6b2
commit 18e4637042
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 7 deletions

View File

@ -0,0 +1,2 @@
bugfixes:
- "proxmox inventory plugin - plugin did not update cache correctly after ``meta: refresh_inventory`` (https://github.com/ansible-collections/community.general/issues/9710, https://github.com/ansible-collections/community.general/pull/9760)."

View File

@ -304,12 +304,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
def _get_json(self, url, ignore_errors=None): def _get_json(self, url, ignore_errors=None):
if not self.use_cache or url not in self._cache.get(self.cache_key, {}):
if self.cache_key not in self._cache:
self._cache[self.cache_key] = {'url': ''}
data = [] data = []
has_data = False
if self.use_cache:
try:
data = self._cache[self.cache_key][url]
has_data = True
except KeyError:
self.update_cache = True
if not has_data:
s = self._get_session() s = self._get_session()
while True: while True:
ret = s.get(url, headers=self.headers) ret = s.get(url, headers=self.headers)
@ -335,9 +340,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
data = data + json['data'] data = data + json['data']
break break
self._cache[self.cache_key][url] = data self._results[url] = data
return make_unsafe(data)
return make_unsafe(self._cache[self.cache_key][url])
def _get_nodes(self): def _get_nodes(self):
return self._get_json("%s/api2/json/nodes" % self.proxmox_url) return self._get_json("%s/api2/json/nodes" % self.proxmox_url)
@ -678,10 +682,14 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
self.exclude_nodes = self.get_option('exclude_nodes') self.exclude_nodes = self.get_option('exclude_nodes')
self.cache_key = self.get_cache_key(path) self.cache_key = self.get_cache_key(path)
self.use_cache = cache and self.get_option('cache') self.use_cache = cache and self.get_option('cache')
self.update_cache = not cache and self.get_option('cache')
self.host_filters = self.get_option('filters') self.host_filters = self.get_option('filters')
self.group_prefix = self.get_option('group_prefix') self.group_prefix = self.get_option('group_prefix')
self.facts_prefix = self.get_option('facts_prefix') self.facts_prefix = self.get_option('facts_prefix')
self.strict = self.get_option('strict') self.strict = self.get_option('strict')
# actually populate inventory # actually populate inventory
self._results = {}
self._populate() self._populate()
if self.update_cache:
self._cache[self.cache_key] = self._results