From b1ef5ef85d01f0c22183fa01e0cc44f548f6b8a0 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 18 Dec 2014 09:45:19 -0500 Subject: Added default environment behavoir for aws credentials --- inventory/multi_ec2.py | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'inventory') diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index 7fbfb0c90..bb4751113 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -15,15 +15,33 @@ class MultiEc2(object): def __init__(self): self.config = None - self.results = {} + self.all_ec2_results = {} self.result = {} self.cache_path_cache = os.path.expanduser('~/.ansible/tmp/multi_ec2_inventory.cache') self.file_path = os.path.join(os.path.dirname(os.path.realpath(__file__))) - + self.config_file = os.path.join(self.file_path,"multi_ec2.yaml") self.parse_cli_args() # load yaml - self.load_yaml_config() + if os.path.isfile(os.path.join(self.file_path,"multi_ec2.yaml")): + self.config = self.load_yaml_config() + elif os.environ.has_key("AWS_ACCESS_KEY_ID") and os.environ.has_key("AWS_SECRET_ACCESS_KEY"): + self.config = {} + self.config['accounts'] = [ + { + 'name': 'default', + 'provider': 'aws/ec2.py', + 'env_vars': { + 'AWS_ACCESS_KEY_ID': os.environ["AWS_ACCESS_KEY_ID"], + 'AWS_SECRET_ACCESS_KEY': os.environ["AWS_SECRET_ACCESS_KEY"], + } + }, + ] + + self.config['cache_max_age'] = 0 + else: + raise RuntimeError("Could not find valid ec2 credentials in the environment.") + # if its a host query, fetch and do not cache if self.args.host: @@ -41,10 +59,14 @@ class MultiEc2(object): respective cloud for inventory. """ config = None + if not conf_file: - conf_file = os.path.join(self.file_path,'multi_ec2.yaml') + conf_file = self.config_file + with open(conf_file) as conf: - self.config = yaml.safe_load(conf) + config = yaml.safe_load(conf) + + return config def get_provider_tags(self,provider, env={}): """Call and query all of the tags that are usuable @@ -103,10 +125,10 @@ class MultiEc2(object): if result['code'] != 0: raise RuntimeError(result['err']) else: - self.results[result['name']] = json.loads(result['out']) - values = self.results.values() + self.all_ec2_results[result['name']] = json.loads(result['out']) + values = self.all_ec2_results.values() values.insert(0, self.result) - map(lambda x: self.merge_destructively(self.result, x), values) + [self.merge_destructively(self.result, x) for x in values] else: # For any 0 result, return it count = 0 -- cgit v1.2.3 From d03b6eb42f160daca4f85b2005ba4c95d44356bd Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 18 Dec 2014 10:04:15 -0500 Subject: Variable-ized the config file path with the name. --- inventory/multi_ec2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'inventory') diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index bb4751113..456bda8c8 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -23,7 +23,7 @@ class MultiEc2(object): self.parse_cli_args() # load yaml - if os.path.isfile(os.path.join(self.file_path,"multi_ec2.yaml")): + if os.path.isfile(self.config_file): self.config = self.load_yaml_config() elif os.environ.has_key("AWS_ACCESS_KEY_ID") and os.environ.has_key("AWS_SECRET_ACCESS_KEY"): self.config = {} -- cgit v1.2.3