diff --git a/lib/ansible/module_utils/ec2.py b/lib/ansible/module_utils/ec2.py new file mode 100644 index 0000000000..3bd1217337 --- /dev/null +++ b/lib/ansible/module_utils/ec2.py @@ -0,0 +1,38 @@ +def get_ec2_creds(module): + + # Check module args for credentials, then check environment vars + + ec2_url = module.params.get('ec2_url') + ec2_secret_key = module.params.get('ec2_secret_key') + ec2_access_key = module.params.get('ec2_access_key') + region = module.params.get('region') + + if not ec2_url: + if 'EC2_URL' in os.environ: + ec2_url = os.environ['EC2_URL'] + elif 'AWS_URL' in os.environ: + ec2_url = os.environ['AWS_URL'] + + if not ec2_access_key: + if 'EC2_ACCESS_KEY' in os.environ: + ec2_access_key = os.environ['EC2_ACCESS_KEY'] + elif 'AWS_ACESS_KEY' in os.environ: + ec2_access_key = os.environ['AWS_ACESS_KEY'] + else: + module.fail_json(msg="Please specify an ec2_access_key") + + if not ec2_secret_key: + if 'EC2_SECRET_KEY' in os.environ: + ec2_secret_key = os.environ['EC2_SECRET_KEY'] + elif 'AWS_SECRET_KEY' in os.environ: + ec2_secret_key = os.environ['AWS_SECRET_KEY'] + else: + module.fail_json(msg="Please specify an ec2_secret_key") + + if not region: + if 'EC2_REGION' in os.environ: + region = os.environ['EC2_REGION'] + elif 'AWS_REGION' in os.environ: + region = os.environ['AWS_REGION'] + + return ec2_url, ec2_access_key, ec2_secret_key, region diff --git a/library/cloud/ec2 b/library/cloud/ec2 index 75b3da960c..e97bcea8e2 100644 --- a/library/cloud/ec2 +++ b/library/cloud/ec2 @@ -573,8 +573,8 @@ def main(): wait = dict(type='bool', default=False), wait_timeout = dict(default=300), ec2_url = dict(), - aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True), - aws_access_key = dict(aliases=['ec2_access_key', 'access_key']), + ec2_secret_key = dict(aliases=['aws_secret_key', 'secret_key'], no_log=True), + ec2_access_key = dict(aliases=['aws_access_key', 'access_key']), placement_group = dict(), user_data = dict(), instance_tags = dict(type='dict'), @@ -586,33 +586,9 @@ def main(): ) ) - ec2_url = module.params.get('ec2_url') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') - region = module.params.get('region') - - - # allow eucarc environment variables to be used if ansible vars aren't set - if not ec2_url and 'EC2_URL' in os.environ: - ec2_url = os.environ['EC2_URL'] - - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) # If we have a region specified, connect to its endpoint. if region: @@ -646,8 +622,8 @@ def main(): module.exit_json(changed=changed, instance_ids=new_instance_ids, instances=instance_dict_array) - -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() diff --git a/library/cloud/ec2_ami b/library/cloud/ec2_ami index 37ccc834bd..a4d8dbf681 100644 --- a/library/cloud/ec2_ami +++ b/library/cloud/ec2_ami @@ -278,32 +278,9 @@ def main(): ) ) - ec2_url = module.params.get('ec2_url') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') - region = module.params.get('region') - - # allow eucarc environment variables to be used if ansible vars aren't set - if not ec2_url and 'EC2_URL' in os.environ: - ec2_url = os.environ['EC2_URL'] - - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) # If we have a region specified, connect to its endpoint. if region: @@ -335,8 +312,9 @@ def main(): create_image(module, ec2) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() diff --git a/library/cloud/ec2_eip b/library/cloud/ec2_eip index 8e45d326b4..4bd3c1b8ad 100644 --- a/library/cloud/ec2_eip +++ b/library/cloud/ec2_eip @@ -258,10 +258,14 @@ def main(): if not boto_found: module.fail_json(msg="boto is required") - ec2 = connect(ec2_url=module.params.get('ec2_url'), - ec2_secret_key=module.params.get('ec2_secret_key'), - ec2_access_key=module.params.get('ec2_access_key'), - region=module.params.get('region')) + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, ec2_access_key, ec2_secret_key, region = get_ec2_creds(module) + + ec2 = connect(ec2_url=ec2_url, + ec2_access_key=ec2_access_key, + ec2_secret_key=ec2_secret_key, + region=region) instance_id = module.params.get('instance_id') public_ip = module.params.get('public_ip') @@ -292,8 +296,9 @@ def main(): -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * if __name__ == '__main__': main() diff --git a/library/cloud/ec2_elb b/library/cloud/ec2_elb index 107eaf807a..df032e5c61 100644 --- a/library/cloud/ec2_elb +++ b/library/cloud/ec2_elb @@ -265,8 +265,10 @@ def main(): ) ) - aws_secret_key = module.params['aws_secret_key'] - aws_access_key = module.params['aws_access_key'] + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) + ec2_elbs = module.params['ec2_elbs'] region = module.params['region'] wait = module.params['wait'] @@ -275,27 +277,6 @@ def main(): if module.params['state'] == 'present' and 'ec2_elbs' not in module.params: module.fail_json(msg="ELBs are required for registration") - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] - - if not region: - module.fail_json(msg=str("Either region or EC2_REGION environment variable must be set.")) - instance_id = module.params['instance_id'] elb_man = ElbManager(module, instance_id, ec2_elbs, aws_access_key, aws_secret_key, region=region) @@ -316,7 +297,8 @@ def main(): module.exit_json(**ec2_facts_result) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() diff --git a/library/cloud/ec2_facts b/library/cloud/ec2_facts index 31d31307c8..1c17fa5b71 100644 --- a/library/cloud/ec2_facts +++ b/library/cloud/ec2_facts @@ -160,7 +160,7 @@ def main(): ) module.exit_json(**ec2_facts_result) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * main() diff --git a/library/cloud/ec2_group b/library/cloud/ec2_group index 648f8f6cdf..0ece964752 100644 --- a/library/cloud/ec2_group +++ b/library/cloud/ec2_group @@ -117,26 +117,19 @@ def main(): ), supports_check_mode=True, ) + + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, ec2_access_key, ec2_secret_key, region = get_ec2_creds(module) + name = module.params['name'] description = module.params['description'] vpc_id = module.params['vpc_id'] rules = module.params['rules'] - ec2_url = module.params.get('ec2_url') - ec2_secret_key = module.params.get('ec2_secret_key') - ec2_access_key = module.params.get('ec2_access_key') - region = module.params.get('region') state = module.params.get('state') changed = False - # allow eucarc environment variables to be used if ansible vars aren't set - if not ec2_url and 'EC2_URL' in os.environ: - ec2_url = os.environ['EC2_URL'] - if not ec2_secret_key and 'EC2_SECRET_KEY' in os.environ: - ec2_secret_key = os.environ['EC2_SECRET_KEY'] - if not ec2_access_key and 'EC2_ACCESS_KEY' in os.environ: - ec2_access_key = os.environ['EC2_ACCESS_KEY'] - # If we have a region specified, connect to its endpoint. if region: try: @@ -251,6 +244,8 @@ def main(): else: module.exit_json(changed=changed, group_id=None) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * + main() diff --git a/library/cloud/ec2_tag b/library/cloud/ec2_tag index 5e8d925055..8f5a5d37c5 100644 --- a/library/cloud/ec2_tag +++ b/library/cloud/ec2_tag @@ -123,36 +123,14 @@ def main(): ) ) + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) + resource = module.params.get('resource') tags = module.params['tags'] - region = module.params.get('region') state = module.params.get('state') - ec2_url = module.params.get('ec2_url') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') - - # allow eucarc environment variables to be used if ansible vars aren't set - if not ec2_url and 'EC2_URL' in os.environ: - ec2_url = os.environ['EC2_URL'] - - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] - + # If we have a region specified, connect to its endpoint. if region: try: @@ -209,7 +187,8 @@ def main(): # }) sys.exit(0) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main() diff --git a/library/cloud/ec2_vol b/library/cloud/ec2_vol index 784f07edd3..6109cdeb27 100644 --- a/library/cloud/ec2_vol +++ b/library/cloud/ec2_vol @@ -146,43 +146,21 @@ def main(): region = dict(aliases=['aws_region', 'ec2_region'], choices=AWS_REGIONS), zone = dict(aliases=['availability_zone', 'aws_zone', 'ec2_zone']), ec2_url = dict(), - aws_secret_key = dict(aliases=['ec2_secret_key', 'secret_key'], no_log=True), - aws_access_key = dict(aliases=['ec2_access_key', 'access_key']), + ec2_secret_key = dict(aliases=['aws_secret_key', 'secret_key'], no_log=True), + ec2_access_key = dict(aliases=['aws_access_key', 'access_key']), ) ) + # def get_ec2_creds(module): + # return ec2_url, ec2_access_key, ec2_secret_key, region + ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module) + instance = module.params.get('instance') volume_size = module.params.get('volume_size') iops = module.params.get('iops') device_name = module.params.get('device_name') - region = module.params.get('region') zone = module.params.get('zone') - ec2_url = module.params.get('ec2_url') - aws_secret_key = module.params.get('aws_secret_key') - aws_access_key = module.params.get('aws_access_key') - - # allow eucarc environment variables to be used if ansible vars aren't set - if not ec2_url and 'EC2_URL' in os.environ: - ec2_url = os.environ['EC2_URL'] - - if not aws_secret_key: - if 'AWS_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['AWS_SECRET_KEY'] - elif 'EC2_SECRET_KEY' in os.environ: - aws_secret_key = os.environ['EC2_SECRET_KEY'] - - if not aws_access_key: - if 'AWS_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['AWS_ACCESS_KEY'] - elif 'EC2_ACCESS_KEY' in os.environ: - aws_access_key = os.environ['EC2_ACCESS_KEY'] - - if not region: - if 'AWS_REGION' in os.environ: - region = os.environ['AWS_REGION'] - elif 'EC2_REGION' in os.environ: - region = os.environ['EC2_REGION'] - + # If we have a region specified, connect to its endpoint. if region: try: @@ -274,7 +252,8 @@ def main(): }) sys.exit(0) -# this is magic, see lib/ansible/module_common.py -#<> +# import module snippets +from ansible.module_utils.basic import * +from ansible.module_utils.ec2 import * main()