From d6ea400efbe132bf33e8bb7fb9ab53d1bfe89a05 Mon Sep 17 00:00:00 2001 From: Will Thames Date: Wed, 1 Feb 2017 00:35:45 +1000 Subject: [PATCH] Improve boto profile handling for EC2 inventory (#20280) Ensure command line profile argument and AWS_PROFILE environment variable overrides config file Remove unnecessary `lambda` function Fix cache file path construction to be more pythonic (and windows-ready) --- contrib/inventory/ec2.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py index 49e3821bcf8..1cc9b833159 100755 --- a/contrib/inventory/ec2.py +++ b/contrib/inventory/ec2.py @@ -340,8 +340,8 @@ class Ec2Inventory(object): else: self.all_elasticache_nodes = False - # boto configuration profile (prefer CLI argument) - self.boto_profile = self.args.boto_profile + # boto configuration profile (prefer CLI argument then environment variables then config file) + self.boto_profile = self.args.boto_profile or os.environ.get('AWS_PROFILE') if config.has_option('ec2', 'boto_profile') and not self.boto_profile: self.boto_profile = config.get('ec2', 'boto_profile') @@ -376,14 +376,11 @@ class Ec2Inventory(object): os.makedirs(cache_dir) cache_name = 'ansible-ec2' - aws_profile = lambda: (self.boto_profile or - os.environ.get('AWS_PROFILE') or - os.environ.get('AWS_ACCESS_KEY_ID') or - self.credentials.get('aws_access_key_id', None)) - if aws_profile(): - cache_name = '%s-%s' % (cache_name, aws_profile()) - self.cache_path_cache = cache_dir + "/%s.cache" % cache_name - self.cache_path_index = cache_dir + "/%s.index" % cache_name + cache_id = self.boto_profile or os.environ.get('AWS_ACCESS_KEY_ID', self.credentials.get('aws_access_key_id')) + if cache_id: + cache_name = '%s-%s' % (cache_name, cache_id) + self.cache_path_cache = os.path.join(cache_dir, "%s.cache" % cache_name) + self.cache_path_index = os.path.join(cache_dir, "%s.index" % cache_name) self.cache_max_age = config.getint('ec2', 'cache_max_age') if config.has_option('ec2', 'expand_csv_tags'):