2013-03-19 17:07:36 +01:00
#!/usr/bin/python
2012-11-09 06:15:12 +01:00
# 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:
2013-02-20 10:31:22 +01:00
- creates ec2 instances and optionally waits for it to be 'running'. This module has a dependency on python-boto.
2012-11-09 06:15:12 +01:00
version_added: "0.9"
options:
2013-01-22 21:09:31 +01:00
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
2013-01-22 21:09:31 +01:00
aliases: ['keypair']
2013-03-16 03:55:01 +01:00
id:
description:
- identifier for this instance or set of instances, so that the module will be idempotent with respect to EC2 instances.
required: false
default: null
aliases: []
2012-11-09 06:15:12 +01:00
group:
description:
2013-02-20 10:31:22 +01:00
- security group to use with the instance
2012-11-09 06:15:12 +01:00
required: false
2013-02-20 10:31:22 +01:00
default: null
aliases: []
group_id:
version_added: "1.1"
description:
- security group id to use with the instance
required: false
default: null
2012-11-09 06:15:12 +01:00
aliases: []
2013-04-08 11:42:34 +02:00
zone:
version_added: "1.2"
description:
- availability zone in which to launch the instance
required: false
default: null
aliases: []
2012-11-09 06:15:12 +01:00
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
2013-02-20 10:31:22 +01:00
required: false
2013-03-12 13:18:12 +01:00
default: "no"
choices: [ "yes", "no" ]
2012-11-09 06:15:12 +01:00
aliases: []
2013-03-07 18:01:55 +01:00
wait_timeout:
2013-03-07 23:37:25 +01:00
description:
2013-03-07 23:44:22 +01:00
- how long before wait gives up, in seconds
2013-03-07 23:37:25 +01:00
default: 300
aliases: []
2012-11-09 06:15:12 +01:00
ec2_url:
description:
2013-02-20 10:31:22 +01:00
- url to use to connect to EC2 or your Eucalyptus cloud (by default the module will use EC2 endpoints)
required: false
2012-11-09 06:15:12 +01:00
default: null
aliases: []
ec2_secret_key:
description:
- ec2 secret key
2013-02-20 10:31:22 +01:00
required: false
2012-11-09 06:15:12 +01:00
default: null
aliases: []
ec2_access_key:
description:
- ec2 access key
2013-02-20 10:31:22 +01:00
required: false
2012-11-09 06:15:12 +01:00
default: null
aliases: []
2013-01-25 16:02:53 +01:00
count:
description:
- number of instances to launch
required: False
default: 1
aliases: []
2013-02-04 19:03:47 +01:00
monitor:
2013-02-20 10:31:22 +01:00
version_added: "1.1"
2013-02-04 19:03:47 +01:00
description:
- enable detailed monitoring (CloudWatch) for instance
2013-02-20 10:31:22 +01:00
required: false
2013-02-04 19:03:47 +01:00
default: null
aliases: []
2012-11-17 16:02:29 +01:00
user_data:
version_added: "0.9"
description:
- opaque blob of data which is made available to the ec2 instance
2013-02-20 10:31:22 +01:00
required: false
2012-11-17 16:02:29 +01:00
default: null
aliases: []
2013-02-09 17:42:14 +01:00
instance_tags:
version_added: "1.0"
description:
2013-02-20 10:31:22 +01:00
- a hash/dictionary of tags to add to the new instance; '{"key":"value"}' and '{"key":"value","key":"value"}'
required: false
2013-02-09 17:42:14 +01:00
default: null
aliases: []
2013-02-23 18:46:35 +01:00
vpc_subnet_id:
2013-02-23 18:45:47 +01:00
version_added: "1.1"
description:
- the subnet ID in which to launch the instance (VPC)
required: false
default: null
aliases: []
2013-04-02 07:31:31 +02:00
private_ip:
2013-04-05 21:30:36 +02:00
version_added: "1.2"
2013-04-02 07:31:31 +02:00
description:
- the private ip address to assign the instance (from the vpc subnet)
required: false
defualt: null
aliases: []
2013-01-22 21:09:31 +01:00
requirements: [ "boto" ]
2013-01-25 16:02:53 +01:00
author: Seth Vidal, Tim Gerla, Lester Wade
2012-11-09 06:15:12 +01:00
'''
2013-04-05 21:30:36 +02:00
EXAMPLES = '''
# Basic provisioning example
local_action:
module: ec2
keypair: mykey
instance_type: c1.medium
image: emi-40603AD1
wait: yes
group: webserver
count: 3
# Advanced example with tagging and CloudWatch
local_action:
module: ec2
keypair: mykey
group: databases
instance_type: m1.large
image: ami-6e649707
wait: yes
wait_timeout: 500
count: 5
instance_tags: '{"db":"postgres"}' monitoring=true'
# VPC example
local_action:
module: ec2
keypair: mykey
group_id: sg-1dc53f72
instance_type: m1.small
image: ami-6e649707
wait: yes
vpc_subnet_id: subnet-29e63245'
'''
2013-01-22 23:29:28 +01:00
import sys
2012-11-09 06:15:12 +01:00
import time
2013-01-22 23:29:28 +01:00
try:
2013-03-07 18:01:55 +01:00
import boto
2013-01-22 23:29:28 +01:00
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(
2013-01-22 21:09:31 +01:00
key_name = dict(required=True, aliases = ['keypair']),
2013-03-16 03:55:01 +01:00
id = dict(),
2013-02-20 10:31:22 +01:00
group = dict(),
group_id = dict(),
2013-04-08 11:42:34 +02:00
zone = dict(),
2012-11-09 06:15:12 +01:00
instance_type = dict(aliases=['type']),
image = dict(required=True),
kernel = dict(),
2013-01-25 16:02:53 +01:00
count = dict(default='1'),
2013-02-09 18:01:11 +01:00
monitoring = dict(choices=BOOLEANS, default=False),
2012-11-09 06:15:12 +01:00
ramdisk = dict(),
wait = dict(choices=BOOLEANS, default=False),
2013-03-07 18:01:55 +01:00
wait_timeout = dict(default=300),
2012-11-09 06:15:12 +01:00
ec2_url = dict(aliases=['EC2_URL']),
2013-02-25 22:34:06 +01:00
ec2_secret_key = dict(aliases=['EC2_SECRET_KEY'], no_log=True),
2012-11-09 06:15:12 +01:00
ec2_access_key = dict(aliases=['EC2_ACCESS_KEY']),
2012-11-17 16:02:29 +01:00
user_data = dict(),
2013-02-09 17:42:14 +01:00
instance_tags = dict(),
2013-02-23 18:45:47 +01:00
vpc_subnet_id = dict(),
2013-04-02 07:31:31 +02:00
private_ip = dict(),
2012-11-09 06:15:12 +01:00
)
)
2013-01-22 21:09:31 +01:00
key_name = module.params.get('key_name')
2013-03-16 03:55:01 +01:00
id = module.params.get('id')
2013-02-20 10:31:22 +01:00
group_name = module.params.get('group')
group_id = module.params.get('group_id')
2013-04-08 11:42:34 +02:00
zone = module.params.get('zone')
2012-11-09 06:15:12 +01:00
instance_type = module.params.get('instance_type')
image = module.params.get('image')
2013-01-25 16:02:53 +01:00
count = module.params.get('count')
2013-02-04 19:03:47 +01:00
monitoring = module.params.get('monitoring')
2012-11-09 06:15:12 +01:00
kernel = module.params.get('kernel')
ramdisk = module.params.get('ramdisk')
wait = module.params.get('wait')
2013-03-07 18:01:55 +01:00
wait_timeout = int(module.params.get('wait_timeout'))
2012-11-09 06:15:12 +01:00
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')
2012-11-17 16:02:29 +01:00
user_data = module.params.get('user_data')
2013-02-09 17:42:14 +01:00
instance_tags = module.params.get('instance_tags')
2013-02-23 18:45:47 +01:00
vpc_subnet_id = module.params.get('vpc_subnet_id')
2013-04-02 07:31:31 +02:00
private_ip = module.params.get('private_ip')
2013-02-09 17:42:14 +01:00
2013-01-22 21:09:31 +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
2013-02-09 18:58:43 +01:00
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))
2013-02-20 10:31:22 +01:00
# Here we try to lookup the group name from the security group id - if group_id is set.
2013-02-09 18:58:43 +01:00
2013-02-20 10:31:22 +01:00
try:
if group_id:
grp_details = ec2.get_all_security_groups(group_ids=group_id)
grp_item = grp_details[0]
group_name = grp_item.name
except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg = str(e))
2013-01-25 16:02:53 +01:00
2013-03-16 03:55:01 +01:00
# Lookup any instances that much our run id.
running_instances = []
count_remaining = int(count)
if id != None:
filter_dict = {'client-token':id, 'instance-state-name' : 'running'}
2013-04-10 22:37:49 +02:00
previous_reservations = ec2.get_all_instances(None, filter_dict)
2013-03-16 03:55:01 +01:00
for res in previous_reservations:
for prev_instance in res.instances:
running_instances.append(prev_instance)
count_remaining = count_remaining - len(running_instances)
2013-01-26 12:26:51 +01:00
# Both min_count and max_count equal count parameter. This means the launch request is explicit (we want count, or fail) in how many instances we want.
2013-02-09 17:42:14 +01:00
2013-03-16 03:55:01 +01:00
if count_remaining > 0:
try:
res = ec2.run_instances(image, key_name = key_name,
client_token=id,
min_count = count_remaining,
max_count = count_remaining,
2013-02-09 18:01:11 +01:00
monitoring_enabled = monitoring,
2013-02-20 10:31:22 +01:00
security_groups = [group_name],
2013-04-08 11:42:34 +02:00
placement = zone,
2013-01-22 21:09:31 +01:00
instance_type = instance_type,
kernel_id = kernel,
ramdisk_id = ramdisk,
2013-02-23 18:45:47 +01:00
subnet_id = vpc_subnet_id,
2013-04-02 07:31:31 +02:00
private_ip_address = private_ip,
2013-01-22 21:09:31 +01:00
user_data = user_data)
2013-03-16 03:55:01 +01:00
except boto.exception.BotoServerError, e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
2012-11-09 06:15:12 +01:00
2013-03-16 03:55:01 +01:00
instids = [ i.id for i in res.instances ]
while True:
try:
res.connection.get_all_instances(instids)
break
except boto.exception.EC2ResponseError as e:
if "<Code>InvalidInstanceID.NotFound</Code>" in str(e):
# there's a race between start and get an instance
continue
else:
module.fail_json(msg = str(e))
2012-11-09 06:15:12 +01:00
2013-03-16 03:55:01 +01:00
if instance_tags:
try:
ec2.create_tags(instids, module.from_json(instance_tags))
except boto.exception.EC2ResponseError as e:
module.fail_json(msg = "%s: %s" % (e.error_code, e.error_message))
2013-02-09 17:42:14 +01:00
2013-03-16 03:55:01 +01:00
# wait here until the instances are up
2013-03-07 18:01:55 +01:00
res_list = res.connection.get_all_instances(instids)
this_res = res_list[0]
2013-03-16 03:55:01 +01:00
num_running = 0
wait_timeout = time.time() + wait_timeout
while wait and wait_timeout > time.time() and 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)
if wait and wait_timeout <= time.time():
# waiting took too long
module.fail_json(msg = "wait for instances running timeout on %s" % time.asctime())
for inst in this_res.instances:
running_instances.append(inst)
instance_dict_array = []
for inst in running_instances:
2012-11-09 06:15:12 +01:00
d = {
'id': inst.id,
2013-03-17 21:51:41 +01:00
'ami_launch_index': inst.ami_launch_index,
'private_ip': inst.private_ip_address,
'private_dns_name': inst.private_dns_name,
2012-11-09 06:15:12 +01:00
'public_ip': inst.ip_address,
2013-03-17 21:51:41 +01:00
'dns_name': inst.dns_name,
'public_dns_name': inst.public_dns_name,
'state_code': inst.state_code,
'architecture': inst.architecture,
'image_id': inst.image_id,
'key_name': inst.key_name,
'virtualization_type': inst.virtualization_type,
'placement': inst.placement,
'kernel': inst.kernel,
'ramdisk': inst.ramdisk,
'launch_time': inst.launch_time,
'instance_type': inst.instance_type,
'root_device_type': inst.root_device_type,
'root_device_name': inst.root_device_name,
'state': inst.state,
'hypervisor': inst.hypervisor
2012-11-09 06:15:12 +01:00
}
2013-03-16 03:55:01 +01:00
instance_dict_array.append(d)
2012-11-09 06:15:12 +01:00
2013-03-16 03:55:01 +01:00
module.exit_json(changed=True, instances=instance_dict_array)
2012-11-09 06:15:12 +01:00
# this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main()