Add region parameter and changed connection code.

pull/2746/head
Lester Wade 12 years ago
parent 66afe13346
commit 8c8014363e

@ -19,7 +19,7 @@ DOCUMENTATION = '''
module: ec2 module: ec2
short_description: create an instance in ec2, return instanceid short_description: create an instance in ec2, return instanceid
description: description:
- creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on python-boto. - creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on python-boto >= 2.5
version_added: "0.9" version_added: "0.9"
options: options:
key_name: key_name:
@ -47,6 +47,13 @@ options:
required: false required: false
default: null default: null
aliases: [] aliases: []
region:
version_added: "1.2"
description:
- the EC2 region to use
required: false
default: null
aliases: []
zone: zone:
version_added: "1.2" version_added: "1.2"
description: description:
@ -191,7 +198,7 @@ import sys
import time import time
try: try:
import boto import boto.ec2
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" print "failed=True msg='boto required for this module'"
sys.exit(1) sys.exit(1)
@ -203,6 +210,7 @@ def main():
id = dict(), id = dict(),
group = dict(), group = dict(),
group_id = dict(), group_id = dict(),
region = dict(choices=['eu-west-1', 'sa-east-1', 'us-east-1', 'ap-northeast-1', 'us-west-2', 'us-west-1', 'ap-southeast-1', 'ap-southeast-2']),
zone = dict(), zone = dict(),
instance_type = dict(aliases=['type']), instance_type = dict(aliases=['type']),
image = dict(required=True), image = dict(required=True),
@ -226,6 +234,7 @@ def main():
id = module.params.get('id') id = module.params.get('id')
group_name = module.params.get('group') group_name = module.params.get('group')
group_id = module.params.get('group_id') group_id = module.params.get('group_id')
region = module.params.get('region')
zone = module.params.get('zone') zone = module.params.get('zone')
instance_type = module.params.get('instance_type') instance_type = module.params.get('instance_type')
image = module.params.get('image') image = module.params.get('image')
@ -251,16 +260,23 @@ def main():
if not ec2_access_key and 'EC2_ACCESS_KEY' in os.environ: if not ec2_access_key and 'EC2_ACCESS_KEY' in os.environ:
ec2_access_key = os.environ['EC2_ACCESS_KEY'] ec2_access_key = os.environ['EC2_ACCESS_KEY']
try: # If we have a region specified, connect to its endpoint.
if ec2_url: # if we have an URL set, connect to the specified endpoint if region:
ec2 = boto.connect_ec2_endpoint(ec2_url, ec2_access_key, ec2_secret_key) try:
else: # otherwise it's Amazon. ec2 = boto.ec2.connect_to_region(region, aws_access_key_id=ec2_access_key, aws_secret_access_key=ec2_secret_key)
ec2 = boto.connect_ec2(ec2_access_key, ec2_secret_key) except boto.exception.NoAuthHandlerFound, e:
except boto.exception.NoAuthHandlerFound, e: module.fail_json(msg = str(e))
module.fail_json(msg = str(e)) # Otherwise, no region so we fallback to the old connection method
else:
try:
if ec2_url: # if we have an URL set, connect to the specified endpoint
ec2 = boto.connect_ec2_endpoint(ec2_url, ec2_access_key, ec2_secret_key)
else: # otherwise it's Amazon.
ec2 = boto.connect_ec2(ec2_access_key, ec2_secret_key)
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e))
# Here we try to lookup the group name from the security group id - if group_id is set. # Here we try to lookup the group name from the security group id - if group_id is set.
try: try:
if group_id: if group_id:
grp_details = ec2.get_all_security_groups(group_ids=group_id) grp_details = ec2.get_all_security_groups(group_ids=group_id)

Loading…
Cancel
Save