Fix AWS EC2 inventory plugin caching of groups (#46961)
* Fix AWS EC2 inventory plugin caching of groups
* Added changelog fragment for aws_ec2 caching fix
* Store the AWS query results
The underlying inventory object contains inventory from other sources,
so caching it as ours would be wrong.
It seems easiest and safest to just cache the boto query results
instead.
* Remove unused functions
(cherry picked from commit 7ba09adee1
)
This commit is contained in:
parent
5a0e016b4b
commit
d130c166ad
2 changed files with 5 additions and 31 deletions
2
changelogs/fragments/46961_fix_aws_ec2_cache.yaml
Normal file
2
changelogs/fragments/46961_fix_aws_ec2_cache.yaml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- aws_ec2 - fixed issue where cache did not contain the computed groups
|
|
@ -422,31 +422,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
self._add_hosts(hosts=groups[group], group=group, hostnames=hostnames)
|
self._add_hosts(hosts=groups[group], group=group, hostnames=hostnames)
|
||||||
self.inventory.add_child('all', group)
|
self.inventory.add_child('all', group)
|
||||||
|
|
||||||
def _populate_from_source(self, source_data):
|
|
||||||
hostvars = source_data.pop('_meta', {}).get('hostvars', {})
|
|
||||||
for group in source_data:
|
|
||||||
if group == 'all':
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
self.inventory.add_group(group)
|
|
||||||
hosts = source_data[group].get('hosts', [])
|
|
||||||
for host in hosts:
|
|
||||||
self._populate_host_vars([host], hostvars.get(host, {}), group)
|
|
||||||
self.inventory.add_child('all', group)
|
|
||||||
|
|
||||||
def _format_inventory(self, groups, hostnames):
|
|
||||||
results = {'_meta': {'hostvars': {}}}
|
|
||||||
for group in groups:
|
|
||||||
results[group] = {'hosts': []}
|
|
||||||
for host in groups[group]:
|
|
||||||
hostname = self._get_hostname(host, hostnames)
|
|
||||||
if not hostname:
|
|
||||||
continue
|
|
||||||
results[group]['hosts'].append(hostname)
|
|
||||||
h = self.inventory.get_host(hostname)
|
|
||||||
results['_meta']['hostvars'][h.name] = h.vars
|
|
||||||
return results
|
|
||||||
|
|
||||||
def _add_hosts(self, hosts, group, hostnames):
|
def _add_hosts(self, hosts, group, hostnames):
|
||||||
'''
|
'''
|
||||||
:param hosts: a list of hosts to be added to a group
|
:param hosts: a list of hosts to be added to a group
|
||||||
|
@ -573,7 +548,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
cache = self.get_option('cache')
|
cache = self.get_option('cache')
|
||||||
|
|
||||||
# Generate inventory
|
# Generate inventory
|
||||||
formatted_inventory = {}
|
|
||||||
cache_needs_update = False
|
cache_needs_update = False
|
||||||
if cache:
|
if cache:
|
||||||
try:
|
try:
|
||||||
|
@ -581,15 +555,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# if cache expires or cache file doesn't exist
|
# if cache expires or cache file doesn't exist
|
||||||
cache_needs_update = True
|
cache_needs_update = True
|
||||||
else:
|
|
||||||
self._populate_from_source(results)
|
|
||||||
|
|
||||||
if not cache or cache_needs_update:
|
if not cache or cache_needs_update:
|
||||||
results = self._query(regions, filters, strict_permissions)
|
results = self._query(regions, filters, strict_permissions)
|
||||||
self._populate(results, hostnames)
|
|
||||||
formatted_inventory = self._format_inventory(results, hostnames)
|
self._populate(results, hostnames)
|
||||||
|
|
||||||
# If the cache has expired/doesn't exist or if refresh_inventory/flush cache is used
|
# If the cache has expired/doesn't exist or if refresh_inventory/flush cache is used
|
||||||
# when the user is using caching, update the cached inventory
|
# when the user is using caching, update the cached inventory
|
||||||
if cache_needs_update or (not cache and self.get_option('cache')):
|
if cache_needs_update or (not cache and self.get_option('cache')):
|
||||||
self.cache.set(cache_key, formatted_inventory)
|
self.cache.set(cache_key, results)
|
||||||
|
|
Loading…
Reference in a new issue