1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

library/ec2:

- use boto instead of euca2ools
- improve error handling/propagation for run failures
- rename keypair to key_name (and preserve alias for old playbooks)
- should be 100% backward compatible with the euca2ools version
This commit is contained in:
Tim Gerla 2013-01-22 12:09:31 -08:00
parent 94a1c221bc
commit 456e6f5a55

View file

@ -22,12 +22,12 @@ description:
- creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on the external I(euca2ools) package. - creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on the external I(euca2ools) package.
version_added: "0.9" version_added: "0.9"
options: options:
keypair: key_name:
description: description:
- key pair to use on the instance - key pair to use on the instance
required: true required: true
default: null default: null
aliases: [] aliases: ['keypair']
group: group:
description: description:
- security group to use on the instance - security group to use on the instance
@ -92,25 +92,18 @@ options:
examples: examples:
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver" - code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver"
description: "Examples from Ansible Playbooks" description: "Examples from Ansible Playbooks"
requirements: [ "euca2ools" ] requirements: [ "boto" ]
author: Seth Vidal author: Seth Vidal
''' '''
import euca2ools.commands.euca.runinstances import boto
import time import time
from urlparse import urlparse
def _run(cmd):
# returns (rc, stdout, stderr) from shell command
process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
stdout, stderr = process.communicate()
return (process.returncode, stdout, stderr)
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
keypair = dict(required=True), key_name = dict(required=True, aliases = ['keypair']),
group = dict(default='default'), group = dict(default='default'),
instance_type = dict(aliases=['type']), instance_type = dict(aliases=['type']),
image = dict(required=True), image = dict(required=True),
@ -125,7 +118,7 @@ def main():
) )
) )
keypair = module.params.get('keypair') key_name = module.params.get('key_name')
group = module.params.get('group') group = module.params.get('group')
instance_type = module.params.get('instance_type') instance_type = module.params.get('instance_type')
image = module.params.get('image') image = module.params.get('image')
@ -138,29 +131,34 @@ def main():
ec2_access_key = module.params.get('ec2_access_key') ec2_access_key = module.params.get('ec2_access_key')
user_data = module.params.get('user_data') user_data = module.params.get('user_data')
if ec2_url: # allow eucarc environment variables to be used if ansible vars aren't set
os.environ['EC2_URL'] = ec2_url if not ec2_url and 'EC2_URL' in os.environ:
if ec2_secret_key: ec2_url = os.environ['EC2_URL']
os.environ['EC2_SECRET_KEY'] = ec2_secret_key if not ec2_secret_key and 'EC2_SECRET_KEY' in os.environ:
if ec2_access_key: ec2_secret_key = os.environ['EC2_SECRET_KEY']
os.environ['EC2_ACCESS_KEY'] = ec2_access_key if not ec2_access_key and 'EC2_ACCESS_KEY' in os.environ:
ec2_access_key = os.environ['EC2_ACCESS_KEY']
if ec2_url: # if we have an URL set, assume Eucalyptus
url = urlparse(ec2_url)
ec2 = boto.connect_euca(host = url.netloc.split(":")[0],
aws_access_key_id = ec2_access_key,
aws_secret_access_key = ec2_secret_key,
port = url.port,
path = url.path)
else: # otherwise it's Amazon.
ec2 = boto.connect_ec2()
# yes I recognize how hacky this is - but it is easier than rewriting try:
# all the try/except's myself. res = ec2.run_instances(image, key_name = key_name,
sys.argv.append(image) min_count = 1, max_count = 1,
eri = euca2ools.commands.euca.runinstances.RunInstances() security_groups = [group],
conn = eri.make_connection() instance_type = instance_type,
res = eri.make_request_cli(conn, 'run_instances', kernel_id = kernel,
image_id=image, ramdisk_id = ramdisk,
min_count=1, user_data = user_data)
max_count=1, except boto.exception.EC2ResponseError as e:
key_name=keypair, module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
security_groups=[group],
instance_type=instance_type,
kernel_id=kernel,
ramdisk_id=ramdisk,
user_data=user_data)
instids = [ i.id for i in res.instances ] instids = [ i.id for i in res.instances ]