summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Oddy <jonathan+github@woaf.net>2018-10-22 14:15:27 +0100
committerToshio Kuratomi <a.badger@gmail.com>2018-10-22 11:59:50 -0700
commitd130c166ad3e571669b4b082e255ab96b35c6d01 (patch)
tree9295a26d46f51b6af8e6bad1d4579f3314fa7955
parent5a0e016b4b52b17ca63fdab2505d62568a22316e (diff)
downloadansible-d130c166ad3e571669b4b082e255ab96b35c6d01.tar.gz
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 7ba09adee1d0ec37524359249af6440ae4a3fe82)
-rw-r--r--changelogs/fragments/46961_fix_aws_ec2_cache.yaml2
-rw-r--r--lib/ansible/plugins/inventory/aws_ec2.py34
2 files changed, 5 insertions, 31 deletions
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 0000000000..f88361bc7a
--- /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 bf2d3188b7..c5aa48e233 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)