2014-03-01 17:06:19 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# 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_lc
|
|
|
|
short_description: Create or delete AWS Autoscaling Launch Configurations
|
|
|
|
description:
|
2016-04-26 22:15:45 +02:00
|
|
|
- Can create or delete AWS Autoscaling Configurations
|
2014-03-01 17:06:19 +01:00
|
|
|
- Works with the ec2_asg module to manage Autoscaling Groups
|
2014-09-19 22:17:46 +02:00
|
|
|
notes:
|
|
|
|
- "Amazon ASG Autoscaling Launch Configurations are immutable once created, so modifying the configuration
|
|
|
|
after it is changed will not modify the launch configuration on AWS. You must create a new config and assign
|
|
|
|
it to the ASG instead."
|
2014-03-01 17:06:19 +01:00
|
|
|
version_added: "1.6"
|
2015-06-15 20:41:22 +02:00
|
|
|
author: "Gareth Rushgrove (@garethr)"
|
2014-03-01 17:06:19 +01:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- register or deregister the instance
|
|
|
|
required: true
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Unique name for configuration
|
|
|
|
required: true
|
2014-04-24 19:47:41 +02:00
|
|
|
instance_type:
|
|
|
|
description:
|
|
|
|
- instance type to use for the instance
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2014-03-01 17:06:19 +01:00
|
|
|
image_id:
|
|
|
|
description:
|
|
|
|
- The AMI unique identifier to be used for the group
|
2014-03-06 03:14:53 +01:00
|
|
|
required: false
|
2014-03-01 17:06:19 +01:00
|
|
|
key_name:
|
|
|
|
description:
|
|
|
|
- The SSH key name to be used for access to managed instances
|
2014-03-06 03:14:53 +01:00
|
|
|
required: false
|
2014-03-01 17:06:19 +01:00
|
|
|
security_groups:
|
|
|
|
description:
|
|
|
|
- A list of security groups into which instances should be found
|
2014-03-06 03:14:53 +01:00
|
|
|
required: false
|
|
|
|
volumes:
|
|
|
|
description:
|
|
|
|
- a list of volume dicts, each containing device name and optionally ephemeral id or snapshot id. Size and type (and number of iops for io device type) must be specified for a new volume or a root volume, and may be passed for a snapshot volume. For any volume, a volume size less than 1 will be interpreted as a request not to create the volume.
|
|
|
|
required: false
|
|
|
|
user_data:
|
|
|
|
description:
|
|
|
|
- opaque blob of data which is made available to the ec2 instance
|
|
|
|
required: false
|
2014-07-01 07:39:06 +02:00
|
|
|
kernel_id:
|
2014-07-01 07:34:55 +02:00
|
|
|
description:
|
|
|
|
- Kernel id for the EC2 instance
|
|
|
|
required: false
|
2014-06-23 09:05:47 +02:00
|
|
|
spot_price:
|
|
|
|
description:
|
|
|
|
- The spot price you are bidding. Only applies for an autoscaling group with spot instances.
|
|
|
|
required: false
|
2014-06-23 10:47:22 +02:00
|
|
|
instance_monitoring:
|
|
|
|
description:
|
|
|
|
- whether instances in group are launched with detailed monitoring.
|
|
|
|
default: false
|
2014-08-05 21:36:17 +02:00
|
|
|
assign_public_ip:
|
|
|
|
description:
|
2014-08-26 20:00:17 +02:00
|
|
|
- Used for Auto Scaling groups that launch instances into an Amazon Virtual Private Cloud. Specifies whether to assign a public IP address to each instance launched in a Amazon VPC.
|
|
|
|
required: false
|
|
|
|
version_added: "1.8"
|
2014-07-01 07:39:06 +02:00
|
|
|
ramdisk_id:
|
|
|
|
description:
|
|
|
|
- A RAM disk id for the instances.
|
|
|
|
required: false
|
2014-08-26 20:00:17 +02:00
|
|
|
version_added: "1.8"
|
2014-07-01 07:47:04 +02:00
|
|
|
instance_profile_name:
|
|
|
|
description:
|
|
|
|
- The name or the Amazon Resource Name (ARN) of the instance profile associated with the IAM role for the instances.
|
|
|
|
required: false
|
2014-08-26 20:00:17 +02:00
|
|
|
version_added: "1.8"
|
2014-07-01 07:50:29 +02:00
|
|
|
ebs_optimized:
|
|
|
|
description:
|
|
|
|
- Specifies whether the instance is optimized for EBS I/O (true) or not (false).
|
2014-08-05 21:36:17 +02:00
|
|
|
required: false
|
|
|
|
default: false
|
|
|
|
version_added: "1.8"
|
2015-06-23 20:08:43 +02:00
|
|
|
classic_link_vpc_id:
|
|
|
|
description:
|
|
|
|
- Id of ClassicLink enabled VPC
|
|
|
|
required: false
|
|
|
|
version_added: "2.0"
|
2015-06-24 17:05:37 +02:00
|
|
|
classic_link_vpc_security_groups:
|
2015-06-23 20:08:43 +02:00
|
|
|
description:
|
2015-09-01 00:09:50 +02:00
|
|
|
- A list of security group id's with which to associate the ClassicLink VPC instances.
|
2015-06-23 20:08:43 +02:00
|
|
|
required: false
|
|
|
|
version_added: "2.0"
|
2015-10-07 22:16:24 +02:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- aws
|
|
|
|
- ec2
|
2016-02-26 23:21:33 +01:00
|
|
|
requires:
|
|
|
|
- "boto >= 2.39.0"
|
2014-03-01 17:06:19 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2014-03-14 17:01:23 +01:00
|
|
|
- ec2_lc:
|
2014-03-01 17:06:19 +01:00
|
|
|
name: special
|
|
|
|
image_id: ami-XXX
|
|
|
|
key_name: default
|
2014-12-01 21:14:57 +01:00
|
|
|
security_groups: ['group', 'group2' ]
|
2014-04-24 19:50:49 +02:00
|
|
|
instance_type: t1.micro
|
2015-06-08 18:01:27 +02:00
|
|
|
volumes:
|
|
|
|
- device_name: /dev/sda1
|
|
|
|
volume_size: 100
|
|
|
|
device_type: io1
|
|
|
|
iops: 3000
|
|
|
|
delete_on_termination: true
|
2016-04-26 15:07:27 +02:00
|
|
|
- device_name: /dev/sdb
|
|
|
|
ephemeral: ephemeral0
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
try:
|
2014-03-06 03:14:53 +01:00
|
|
|
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
|
2014-03-01 17:06:19 +01:00
|
|
|
import boto.ec2.autoscale
|
|
|
|
from boto.ec2.autoscale import LaunchConfiguration
|
|
|
|
from boto.exception import BotoServerError
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = True
|
2014-03-01 17:06:19 +01:00
|
|
|
except ImportError:
|
2015-04-02 01:16:54 +02:00
|
|
|
HAS_BOTO = False
|
2014-03-01 17:06:19 +01:00
|
|
|
|
2014-03-06 03:14:53 +01:00
|
|
|
|
|
|
|
def create_block_device(module, volume):
|
|
|
|
# Not aware of a way to determine this programatically
|
|
|
|
# http://aws.amazon.com/about-aws/whats-new/2013/10/09/ebs-provisioned-iops-maximum-iops-gb-ratio-increased-to-30-1/
|
|
|
|
MAX_IOPS_TO_SIZE_RATIO = 30
|
|
|
|
if 'snapshot' not in volume and 'ephemeral' not in volume:
|
|
|
|
if 'volume_size' not in volume:
|
|
|
|
module.fail_json(msg='Size must be specified when creating a new volume or modifying the root volume')
|
|
|
|
if 'snapshot' in volume:
|
|
|
|
if 'device_type' in volume and volume.get('device_type') == 'io1' and 'iops' not in volume:
|
|
|
|
module.fail_json(msg='io1 volumes must have an iops value set')
|
|
|
|
if 'ephemeral' in volume:
|
|
|
|
if 'snapshot' in volume:
|
|
|
|
module.fail_json(msg='Cannot set both ephemeral and snapshot')
|
|
|
|
return BlockDeviceType(snapshot_id=volume.get('snapshot'),
|
|
|
|
ephemeral_name=volume.get('ephemeral'),
|
|
|
|
size=volume.get('volume_size'),
|
|
|
|
volume_type=volume.get('device_type'),
|
|
|
|
delete_on_termination=volume.get('delete_on_termination', False),
|
|
|
|
iops=volume.get('iops'))
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_launch_config(connection, module):
|
|
|
|
name = module.params.get('name')
|
|
|
|
image_id = module.params.get('image_id')
|
|
|
|
key_name = module.params.get('key_name')
|
|
|
|
security_groups = module.params['security_groups']
|
2014-03-06 03:14:53 +01:00
|
|
|
user_data = module.params.get('user_data')
|
|
|
|
volumes = module.params['volumes']
|
|
|
|
instance_type = module.params.get('instance_type')
|
2014-06-23 09:05:47 +02:00
|
|
|
spot_price = module.params.get('spot_price')
|
2014-06-23 10:47:22 +02:00
|
|
|
instance_monitoring = module.params.get('instance_monitoring')
|
2014-08-05 21:36:17 +02:00
|
|
|
assign_public_ip = module.params.get('assign_public_ip')
|
2014-07-01 07:39:06 +02:00
|
|
|
kernel_id = module.params.get('kernel_id')
|
|
|
|
ramdisk_id = module.params.get('ramdisk_id')
|
2014-07-01 07:47:04 +02:00
|
|
|
instance_profile_name = module.params.get('instance_profile_name')
|
2014-07-01 07:50:29 +02:00
|
|
|
ebs_optimized = module.params.get('ebs_optimized')
|
2015-06-23 20:08:43 +02:00
|
|
|
classic_link_vpc_id = module.params.get('classic_link_vpc_id')
|
|
|
|
classic_link_vpc_security_groups = module.params.get('classic_link_vpc_security_groups')
|
2014-03-06 03:14:53 +01:00
|
|
|
bdm = BlockDeviceMapping()
|
|
|
|
|
|
|
|
if volumes:
|
|
|
|
for volume in volumes:
|
|
|
|
if 'device_name' not in volume:
|
|
|
|
module.fail_json(msg='Device name must be set for volume')
|
|
|
|
# Minimum volume size is 1GB. We'll use volume size explicitly set to 0
|
|
|
|
# to be a signal not to create this volume
|
|
|
|
if 'volume_size' not in volume or int(volume['volume_size']) > 0:
|
|
|
|
bdm[volume['device_name']] = create_block_device(module, volume)
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
lc = LaunchConfiguration(
|
|
|
|
name=name,
|
|
|
|
image_id=image_id,
|
|
|
|
key_name=key_name,
|
2014-03-06 03:14:53 +01:00
|
|
|
security_groups=security_groups,
|
|
|
|
user_data=user_data,
|
|
|
|
block_device_mappings=[bdm],
|
2014-06-23 09:05:47 +02:00
|
|
|
instance_type=instance_type,
|
2014-07-01 07:39:06 +02:00
|
|
|
kernel_id=kernel_id,
|
2014-06-23 10:47:22 +02:00
|
|
|
spot_price=spot_price,
|
2014-08-05 21:36:17 +02:00
|
|
|
instance_monitoring=instance_monitoring,
|
2015-06-23 20:08:43 +02:00
|
|
|
associate_public_ip_address=assign_public_ip,
|
2014-07-01 07:47:04 +02:00
|
|
|
ramdisk_id=ramdisk_id,
|
2014-07-01 07:50:29 +02:00
|
|
|
instance_profile_name=instance_profile_name,
|
2014-07-01 07:53:30 +02:00
|
|
|
ebs_optimized=ebs_optimized,
|
2015-06-23 20:08:43 +02:00
|
|
|
classic_link_vpc_security_groups=classic_link_vpc_security_groups,
|
|
|
|
classic_link_vpc_id=classic_link_vpc_id,
|
2014-08-26 20:00:17 +02:00
|
|
|
)
|
2014-03-06 03:14:53 +01:00
|
|
|
|
|
|
|
launch_configs = connection.get_all_launch_configurations(names=[name])
|
|
|
|
changed = False
|
|
|
|
if not launch_configs:
|
|
|
|
try:
|
|
|
|
connection.create_launch_configuration(lc)
|
|
|
|
launch_configs = connection.get_all_launch_configurations(names=[name])
|
|
|
|
changed = True
|
2016-06-02 21:56:48 +02:00
|
|
|
except BotoServerError as e:
|
2014-03-06 03:14:53 +01:00
|
|
|
module.fail_json(msg=str(e))
|
2015-07-27 19:40:27 +02:00
|
|
|
|
|
|
|
result = dict(
|
2015-08-06 17:41:23 +02:00
|
|
|
((a[0], a[1]) for a in vars(launch_configs[0]).items()
|
2015-08-07 21:27:57 +02:00
|
|
|
if a[0] not in ('connection', 'created_time', 'instance_monitoring', 'block_device_mappings'))
|
2015-08-06 17:41:23 +02:00
|
|
|
)
|
2015-08-01 05:57:14 +02:00
|
|
|
result['created_time'] = str(launch_configs[0].created_time)
|
2015-08-06 17:41:23 +02:00
|
|
|
# Looking at boto's launchconfig.py, it looks like this could be a boolean
|
|
|
|
# value or an object with an enabled attribute. The enabled attribute
|
|
|
|
# could be a boolean or a string representation of a boolean. Since
|
|
|
|
# I can't test all permutations myself to see if my reading of the code is
|
|
|
|
# correct, have to code this *very* defensively
|
|
|
|
if launch_configs[0].instance_monitoring is True:
|
|
|
|
result['instance_monitoring'] = True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
result['instance_monitoring'] = module.boolean(launch_configs[0].instance_monitoring.enabled)
|
|
|
|
except AttributeError:
|
|
|
|
result['instance_monitoring'] = False
|
2015-08-07 21:27:57 +02:00
|
|
|
if launch_configs[0].block_device_mappings is not None:
|
|
|
|
result['block_device_mappings'] = []
|
|
|
|
for bdm in launch_configs[0].block_device_mappings:
|
|
|
|
result['block_device_mappings'].append(dict(device_name=bdm.device_name, virtual_name=bdm.virtual_name))
|
|
|
|
if bdm.ebs is not None:
|
|
|
|
result['block_device_mappings'][-1]['ebs'] = dict(snapshot_id=bdm.ebs.snapshot_id, volume_size=bdm.ebs.volume_size)
|
|
|
|
|
2014-03-06 03:14:53 +01:00
|
|
|
|
2015-08-01 05:57:14 +02:00
|
|
|
module.exit_json(changed=changed, name=result['name'], created_time=result['created_time'],
|
|
|
|
image_id=result['image_id'], arn=result['launch_configuration_arn'],
|
|
|
|
security_groups=result['security_groups'],
|
|
|
|
instance_type=result['instance_type'],
|
2015-07-14 18:02:03 +02:00
|
|
|
result=result)
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
def delete_launch_config(connection, module):
|
|
|
|
name = module.params.get('name')
|
2014-03-06 03:14:53 +01:00
|
|
|
launch_configs = connection.get_all_launch_configurations(names=[name])
|
2014-03-01 17:06:19 +01:00
|
|
|
if launch_configs:
|
|
|
|
launch_configs[0].delete()
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2014-03-06 03:14:53 +01:00
|
|
|
argument_spec = ec2_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
|
|
|
name=dict(required=True, type='str'),
|
|
|
|
image_id=dict(type='str'),
|
|
|
|
key_name=dict(type='str'),
|
|
|
|
security_groups=dict(type='list'),
|
|
|
|
user_data=dict(type='str'),
|
2014-07-01 07:39:06 +02:00
|
|
|
kernel_id=dict(type='str'),
|
2014-03-06 03:14:53 +01:00
|
|
|
volumes=dict(type='list'),
|
|
|
|
instance_type=dict(type='str'),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2014-06-23 09:05:47 +02:00
|
|
|
spot_price=dict(type='float'),
|
2014-07-01 07:39:06 +02:00
|
|
|
ramdisk_id=dict(type='str'),
|
2014-07-01 07:47:04 +02:00
|
|
|
instance_profile_name=dict(type='str'),
|
2014-07-01 07:50:29 +02:00
|
|
|
ebs_optimized=dict(default=False, type='bool'),
|
2014-07-01 07:53:30 +02:00
|
|
|
associate_public_ip_address=dict(type='bool'),
|
2014-06-23 10:47:22 +02:00
|
|
|
instance_monitoring=dict(default=False, type='bool'),
|
2015-06-23 20:08:43 +02:00
|
|
|
assign_public_ip=dict(type='bool'),
|
|
|
|
classic_link_vpc_security_groups=dict(type='list'),
|
|
|
|
classic_link_vpc_id=dict(type='str')
|
2014-03-01 17:06:19 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-03-06 03:14:53 +01:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|
2015-04-02 01:16:54 +02:00
|
|
|
if not HAS_BOTO:
|
|
|
|
module.fail_json(msg='boto required for this module')
|
|
|
|
|
2014-03-06 03:14:53 +01:00
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
try:
|
2014-03-06 03:14:53 +01:00
|
|
|
connection = connect_to_aws(boto.ec2.autoscale, region, **aws_connect_params)
|
2016-06-02 21:56:48 +02:00
|
|
|
except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
|
2014-03-06 03:14:53 +01:00
|
|
|
module.fail_json(msg=str(e))
|
2014-03-01 17:06:19 +01:00
|
|
|
|
|
|
|
state = module.params.get('state')
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
create_launch_config(connection, module)
|
|
|
|
elif state == 'absent':
|
|
|
|
delete_launch_config(connection, module)
|
|
|
|
|
|
|
|
main()
|