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

202 lines
6.4 KiB
Text
Raw Normal View History

2012-11-09 06:15:12 +01:00
#!/usr/bin/python -tt
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
DOCUMENTATION = '''
---
module: ec2
short_description: create an instance in ec2, return instanceid
description:
2012-11-21 18:49:30 +01:00
- creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on the external I(euca2ools) package.
2012-11-09 06:15:12 +01:00
version_added: "0.9"
options:
key_name:
2012-11-09 06:15:12 +01:00
description:
2012-11-21 18:49:30 +01:00
- key pair to use on the instance
2012-11-09 06:15:12 +01:00
required: true
default: null
aliases: ['keypair']
2012-11-09 06:15:12 +01:00
group:
description:
- security group to use on the instance
required: false
default: 'default'
aliases: []
instance_type:
description:
- instance type to use for the instance
required: true
default: null
aliases: []
image:
description:
2012-11-21 18:49:30 +01:00
- I(emi) (or I(ami)) to use for the instance
2012-11-09 06:15:12 +01:00
required: true
default: null
aliases: []
kernel:
description:
2012-11-21 18:49:30 +01:00
- kernel I(eki) to use for the instance
2012-11-09 06:15:12 +01:00
required: false
default: null
aliases: []
ramdisk:
description:
2012-11-21 18:49:30 +01:00
- ramdisk I(eri) to use for the instance
2012-11-09 06:15:12 +01:00
required: false
default: null
aliases: []
wait:
description:
- wait for the instance to be in state 'running' before returning
required: False
default: False
aliases: []
ec2_url:
description:
- url to use to connect to ec2 or your Eucalyptus cloud (for example (https://ec2.amazonaws.com) when using Amazon ec2 directly and not Eucalyptus)
2012-11-09 06:15:12 +01:00
required: False
default: null
aliases: []
ec2_secret_key:
description:
- ec2 secret key
required: False
default: null
aliases: []
ec2_access_key:
description:
- ec2 access key
required: False
default: null
aliases: []
count:
description:
- number of instances to launch
required: False
default: 1
aliases: []
user_data:
version_added: "0.9"
description:
- opaque blob of data which is made available to the ec2 instance
required: False
default: null
aliases: []
2012-11-09 06:15:12 +01:00
examples:
- code: "local_action: ec2 keypair=admin instance_type=m1.large image=emi-40603AD1 wait=true group=webserver count=3"
2012-11-09 06:15:12 +01:00
description: "Examples from Ansible Playbooks"
requirements: [ "boto" ]
author: Seth Vidal, Tim Gerla, Lester Wade
2012-11-09 06:15:12 +01:00
'''
import sys
2012-11-09 06:15:12 +01:00
import time
try:
import boto
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
2012-11-09 06:15:12 +01:00
def main():
module = AnsibleModule(
argument_spec = dict(
key_name = dict(required=True, aliases = ['keypair']),
2012-11-09 06:15:12 +01:00
group = dict(default='default'),
instance_type = dict(aliases=['type']),
image = dict(required=True),
kernel = dict(),
count = dict(default='1'),
2012-11-09 06:15:12 +01:00
ramdisk = dict(),
wait = dict(choices=BOOLEANS, default=False),
ec2_url = dict(aliases=['EC2_URL']),
ec2_secret_key = dict(aliases=['EC2_SECRET_KEY']),
ec2_access_key = dict(aliases=['EC2_ACCESS_KEY']),
user_data = dict(),
2012-11-09 06:15:12 +01:00
)
)
key_name = module.params.get('key_name')
2012-11-09 06:15:12 +01:00
group = module.params.get('group')
instance_type = module.params.get('instance_type')
image = module.params.get('image')
count = module.params.get('count')
2012-11-09 06:15:12 +01:00
kernel = module.params.get('kernel')
ramdisk = module.params.get('ramdisk')
wait = module.params.get('wait')
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')
user_data = module.params.get('user_data')
2012-11-09 06:15:12 +01:00
# 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']
2012-11-09 06:15:12 +01:00
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)
# Note min_count is static in value. Since we aren't interested in addressing an autoscaling use-case.
# Autoscaling means more instances are launched on a triggered event, so this is post-play/run stuff.
2012-11-09 06:15:12 +01:00
try:
res = ec2.run_instances(image, key_name = key_name,
min_count = count, max_count = count,
security_groups = [group],
instance_type = instance_type,
kernel_id = kernel,
ramdisk_id = ramdisk,
user_data = user_data)
except boto.exception.EC2ResponseError as e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
2012-11-09 06:15:12 +01:00
instids = [ i.id for i in res.instances ]
res_list = res.connection.get_all_instances(instids)
this_res = res_list[0]
if wait:
# wait here until the instances are up
num_running = 0
while num_running != len(instids):
res_list = res.connection.get_all_instances(instids)
this_res = res_list[0]
num_running = len([ i for i in this_res.instances if i.state=='running' ])
time.sleep(5)
2012-11-09 06:15:12 +01:00
instances = []
for inst in this_res.instances:
d = {
'id': inst.id,
'public_ip': inst.ip_address,
2013-01-10 16:21:18 +01:00
'public_dns_name': inst.public_dns_name
2012-11-09 06:15:12 +01:00
}
instances.append(d)
result = {"changed": True,
"instances": instances }
module.exit_json(**result)
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()