From b54e8f81469807cdd6cc57d3c03b22ee1212b4cc Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 7 May 2015 11:09:00 -0400 Subject: Allow option in multi_ec2 to set cache location. --- inventory/multi_ec2.yaml.example | 3 +++ 1 file changed, 3 insertions(+) (limited to 'inventory/multi_ec2.yaml.example') diff --git a/inventory/multi_ec2.yaml.example b/inventory/multi_ec2.yaml.example index 91e7c7970..d8361a49f 100644 --- a/inventory/multi_ec2.yaml.example +++ b/inventory/multi_ec2.yaml.example @@ -1,4 +1,7 @@ # multi ec2 inventory configs +# +cache_location: ~/.ansible/tmp/multi_ec2_inventory.cache + accounts: - name: aws1 provider: aws/hosts/ec2.py -- cgit v1.2.3 From 7c905c0cf962ec9b7f5bd140a506bd614831f0e8 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 12 May 2015 12:20:11 -0400 Subject: Added capability to pass in ec2.ini file. --- inventory/multi_ec2.py | 74 ++++++++++++++++++++++++++++------------ inventory/multi_ec2.yaml.example | 10 ++++++ 2 files changed, 63 insertions(+), 21 deletions(-) (limited to 'inventory/multi_ec2.yaml.example') diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index 063a80300..d251c6a6a 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -13,7 +13,7 @@ import subprocess import json import errno import fcntl - +import tempfile CONFIG_FILE_NAME = 'multi_ec2.yaml' DEFAULT_CACHE_PATH = os.path.expanduser('~/.ansible/tmp/multi_ec2_inventory.cache') @@ -128,6 +128,54 @@ class MultiEc2(object): return subprocess.Popen(cmds, stderr=subprocess.PIPE, \ stdout=subprocess.PIPE, env=env) + + @staticmethod + def generate_config(config_data): + """Generate the ec2.ini file in as a secure temp file. + Once generated, pass it to the ec2.py as an environment variable. + """ + fildes, tmp_file_path = tempfile.mkstemp(prefix='multi_ec2.ini.') + for section, values in config_data.items(): + os.write(fildes, "[%s]\n" % section) + for option, value in values.items(): + os.write(fildes, "%s = %s\n" % (option, value)) + os.close(fildes) + return tmp_file_path + + def run_provider(self): + '''Setup the provider call with proper variables + and call self.get_provider_tags. + ''' + try: + all_results = [] + tmp_file_path = None + processes = {} + for account in self.config['accounts']: + env = account['env_vars'] + if account.has_key('provider_config'): + tmp_file_path = MultiEc2.generate_config(account['provider_config']) + env['EC2_INI_PATH'] = tmp_file_path + name = account['name'] + provider = account['provider'] + processes[name] = self.get_provider_tags(provider, env) + + # for each process collect stdout when its available + for name, process in processes.items(): + out, err = process.communicate() + all_results.append({ + "name": name, + "out": out.strip(), + "err": err.strip(), + "code": process.returncode + }) + + finally: + # Clean up the mkstemp file + if tmp_file_path: + os.unlink(tmp_file_path) + + return all_results + def get_inventory(self): """Create the subprocess to fetch tags from a provider. Host query: @@ -138,28 +186,12 @@ class MultiEc2(object): Query all of the different accounts for their tags. Once completed store all of their results into one merged updated hash. """ - processes = {} - for account in self.config['accounts']: - env = account['env_vars'] - name = account['name'] - provider = account['provider'] - processes[name] = self.get_provider_tags(provider, env) - - # for each process collect stdout when its available - all_results = [] - for name, process in processes.items(): - out, err = process.communicate() - all_results.append({ - "name": name, - "out": out.strip(), - "err": err.strip(), - "code": process.returncode - }) + provider_results = self.run_provider() # process --host results if not self.args.host: # For any non-zero, raise an error on it - for result in all_results: + for result in provider_results: if result['code'] != 0: raise RuntimeError(result['err']) else: @@ -171,9 +203,9 @@ class MultiEc2(object): else: # For any 0 result, return it count = 0 - for results in all_results: + for results in provider_results: if results['code'] == 0 and results['err'] == '' and results['out'] != '{}': - self.result = json.loads(out) + self.result = json.loads(results['out']) count += 1 if count > 1: raise RuntimeError("Found > 1 results for --host %s. \ diff --git a/inventory/multi_ec2.yaml.example b/inventory/multi_ec2.yaml.example index d8361a49f..c41c134ab 100644 --- a/inventory/multi_ec2.yaml.example +++ b/inventory/multi_ec2.yaml.example @@ -5,6 +5,15 @@ cache_location: ~/.ansible/tmp/multi_ec2_inventory.cache accounts: - name: aws1 provider: aws/hosts/ec2.py + provider_config: + ec2: + regions: all + regions_exclude: us-gov-west-1,cn-north-1 + destination_variable: public_dns_name + route53: False + cache_path: ~/.ansible/tmp + cache_max_age: 300 + vpc_destination_variable: ip_address env_vars: AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX @@ -14,5 +23,6 @@ accounts: env_vars: AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + EC2_INI_PATH: /etc/ansible/ec2.ini cache_max_age: 60 -- cgit v1.2.3 From d82c71ce9a98c1e9ecabf24cd7bd7c7e19aabec2 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Wed, 13 May 2015 16:38:20 -0400 Subject: Added support for grouping and a bug fix. --- inventory/multi_ec2.py | 64 ++++++++++++++++++++++++++++++---------- inventory/multi_ec2.yaml.example | 6 +++- 2 files changed, 53 insertions(+), 17 deletions(-) (limited to 'inventory/multi_ec2.yaml.example') diff --git a/inventory/multi_ec2.py b/inventory/multi_ec2.py index d251c6a6a..11247f942 100755 --- a/inventory/multi_ec2.py +++ b/inventory/multi_ec2.py @@ -14,6 +14,7 @@ import json import errno import fcntl import tempfile +import copy CONFIG_FILE_NAME = 'multi_ec2.yaml' DEFAULT_CACHE_PATH = os.path.expanduser('~/.ansible/tmp/multi_ec2_inventory.cache') @@ -148,13 +149,13 @@ class MultiEc2(object): ''' try: all_results = [] - tmp_file_path = None + tmp_file_paths = [] processes = {} for account in self.config['accounts']: env = account['env_vars'] if account.has_key('provider_config'): - tmp_file_path = MultiEc2.generate_config(account['provider_config']) - env['EC2_INI_PATH'] = tmp_file_path + tmp_file_paths.append(MultiEc2.generate_config(account['provider_config'])) + env['EC2_INI_PATH'] = tmp_file_paths[-1] name = account['name'] provider = account['provider'] processes[name] = self.get_provider_tags(provider, env) @@ -171,8 +172,8 @@ class MultiEc2(object): finally: # Clean up the mkstemp file - if tmp_file_path: - os.unlink(tmp_file_path) + for tmp_file in tmp_file_paths: + os.unlink(tmp_file) return all_results @@ -189,27 +190,58 @@ class MultiEc2(object): provider_results = self.run_provider() # process --host results - if not self.args.host: + # For any 0 result, return it + if self.args.host: + count = 0 + for results in provider_results: + if results['code'] == 0 and results['err'] == '' and results['out'] != '{}': + self.result = json.loads(results['out']) + count += 1 + if count > 1: + raise RuntimeError("Found > 1 results for --host %s. \ + This is an invalid state." % self.args.host) + # process --list results + else: # For any non-zero, raise an error on it for result in provider_results: if result['code'] != 0: raise RuntimeError(result['err']) else: self.all_ec2_results[result['name']] = json.loads(result['out']) + + # Check if user wants extra vars in yaml by + # having hostvars and all_group defined + for acc_config in self.config['accounts']: + self.apply_account_config(acc_config) + + # Build results by merging all dictionaries values = self.all_ec2_results.values() values.insert(0, self.result) for result in values: MultiEc2.merge_destructively(self.result, result) - else: - # For any 0 result, return it - count = 0 - for results in provider_results: - if results['code'] == 0 and results['err'] == '' and results['out'] != '{}': - self.result = json.loads(results['out']) - count += 1 - if count > 1: - raise RuntimeError("Found > 1 results for --host %s. \ - This is an invalid state." % self.args.host) + + def apply_account_config(self, acc_config): + ''' Apply account config settings + ''' + if not acc_config.has_key('hostvars') and not acc_config.has_key('all_group'): + return + + results = self.all_ec2_results[acc_config['name']] + # Update each hostvar with the newly desired key: value + for host_property, value in acc_config['hostvars'].items(): + # Verify the account results look sane + # by checking for these keys ('_meta' and 'hostvars' exist) + if results.has_key('_meta') and results['_meta'].has_key('hostvars'): + for data in results['_meta']['hostvars'].values(): + data[str(host_property)] = str(value) + + # Add this group + results["%s_%s" % (host_property, value)] = \ + copy.copy(results[acc_config['all_group']]) + + # store the results back into all_ec2_results + self.all_ec2_results[acc_config['name']] = results + @staticmethod def merge_destructively(input_a, input_b): "merges b into input_a" diff --git a/inventory/multi_ec2.yaml.example b/inventory/multi_ec2.yaml.example index c41c134ab..99f157b11 100644 --- a/inventory/multi_ec2.yaml.example +++ b/inventory/multi_ec2.yaml.example @@ -17,8 +17,12 @@ accounts: env_vars: AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + all_group: ec2 + hostvars: + cloud: aws + account: aws1 - - name: aws2 +- name: aws2 provider: aws/hosts/ec2.py env_vars: AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXXXXXXX -- cgit v1.2.3