From 7ba09adee1d0ec37524359249af6440ae4a3fe82 Mon Sep 17 00:00:00 2001 From: Jonathan Oddy Date: Mon, 22 Oct 2018 14:15:27 +0100 Subject: [PATCH] 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 --- .../fragments/46961_fix_aws_ec2_cache.yaml | 2 ++ lib/ansible/plugins/inventory/aws_ec2.py | 34 ++----------------- 2 files changed, 5 insertions(+), 31 deletions(-) create mode 100644 changelogs/fragments/46961_fix_aws_ec2_cache.yaml diff --git a/changelogs/fragments/46961_fix_aws_ec2_cache.yaml b/changelogs/fragments/46961_fix_aws_ec2_cache.yaml new file mode 100644 index 00000000000..f88361bc7ab --- /dev/null +++ b/changelogs/fragments/46961_fix_aws_ec2_cache.yaml @@ -0,0 +1,2 @@ +bugfixes: + - aws_ec2 - fixed issue where cache did not contain the computed groups diff --git a/lib/ansible/plugins/inventory/aws_ec2.py b/lib/ansible/plugins/inventory/aws_ec2.py index 95ca539ed27..da4ad1c58a9 100644 --- a/lib/ansible/plugins/inventory/aws_ec2.py +++ b/lib/ansible/plugins/inventory/aws_ec2.py @@ -422,31 +422,6 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): self._add_hosts(hosts=groups[group], group=group, hostnames=hostnames) 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): ''' :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') # Generate inventory - formatted_inventory = {} cache_needs_update = False if cache: try: @@ -581,15 +555,13 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): except KeyError: # if cache expires or cache file doesn't exist cache_needs_update = True - else: - self._populate_from_source(results) if not cache or cache_needs_update: 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 # when the user is using caching, update the cached inventory 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)