|
|
|
@ -67,6 +67,12 @@ options:
|
|
|
|
|
- List of VPC subnets to use
|
|
|
|
|
required: false
|
|
|
|
|
default: None
|
|
|
|
|
tags:
|
|
|
|
|
description:
|
|
|
|
|
- List of tag dictionaries to use. Required keys are 'key', 'value'. Optional key is 'propagate_at_launch', which defaults to true.
|
|
|
|
|
required: false
|
|
|
|
|
default: None
|
|
|
|
|
version_added: "1.7"
|
|
|
|
|
extends_documentation_fragment: aws
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
@ -80,6 +86,11 @@ EXAMPLES = '''
|
|
|
|
|
max_size: 10
|
|
|
|
|
desired_capacity: 5
|
|
|
|
|
vpc_zone_identifier: 'subnet-abcd1234,subnet-1a2b3c4d'
|
|
|
|
|
tags:
|
|
|
|
|
- key: environment
|
|
|
|
|
value: production
|
|
|
|
|
propagate_at_launch: no
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
@ -90,12 +101,14 @@ from ansible.module_utils.ec2 import *
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import boto.ec2.autoscale
|
|
|
|
|
from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup
|
|
|
|
|
from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup, Tag
|
|
|
|
|
from boto.exception import BotoServerError
|
|
|
|
|
except ImportError:
|
|
|
|
|
print "failed=True msg='boto required for this module'"
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
ASG_ATTRIBUTES = ('launch_config_name', 'max_size', 'min_size', 'desired_capacity',
|
|
|
|
|
'vpc_zone_identifier', 'availability_zones')
|
|
|
|
|
|
|
|
|
|
def enforce_required_arguments(module):
|
|
|
|
|
''' As many arguments are not required for autoscale group deletion
|
|
|
|
@ -109,8 +122,15 @@ def enforce_required_arguments(module):
|
|
|
|
|
module.fail_json(msg="Missing required arguments for autoscaling group create/update: %s" % ",".join(missing_args))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_properties(autoscaling_group):
|
|
|
|
|
properties = dict((attr, getattr(autoscaling_group, attr)) for attr in ASG_ATTRIBUTES)
|
|
|
|
|
if autoscaling_group.instances:
|
|
|
|
|
properties['instances'] = [i.instance_id for i in autoscaling_group.instances]
|
|
|
|
|
properties['load_balancers'] = autoscaling_group.load_balancers
|
|
|
|
|
return properties
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_autoscaling_group(connection, module):
|
|
|
|
|
enforce_required_arguments(module)
|
|
|
|
|
|
|
|
|
|
group_name = module.params.get('name')
|
|
|
|
|
load_balancers = module.params['load_balancers']
|
|
|
|
@ -120,8 +140,7 @@ def create_autoscaling_group(connection, module):
|
|
|
|
|
max_size = module.params['max_size']
|
|
|
|
|
desired_capacity = module.params.get('desired_capacity')
|
|
|
|
|
vpc_zone_identifier = module.params.get('vpc_zone_identifier')
|
|
|
|
|
|
|
|
|
|
launch_configs = connection.get_all_launch_configurations(names=[launch_config_name])
|
|
|
|
|
set_tags = module.params.get('tags')
|
|
|
|
|
|
|
|
|
|
as_groups = connection.get_all_groups(names=[group_name])
|
|
|
|
|
|
|
|
|
@ -131,9 +150,19 @@ def create_autoscaling_group(connection, module):
|
|
|
|
|
ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
|
|
|
|
|
except boto.exception.NoAuthHandlerFound, e:
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
availability_zones = module.params['availability_zones'] = [zone.name for zone in ec2_connection.get_all_zones()]
|
|
|
|
|
|
|
|
|
|
asg_tags = []
|
|
|
|
|
for tag in set_tags:
|
|
|
|
|
asg_tags.append(Tag(key=tag.get('key'),
|
|
|
|
|
value=tag.get('value'),
|
|
|
|
|
propagate_at_launch=bool(tag.get('propagate_at_launch', True)),
|
|
|
|
|
resource_id=group_name))
|
|
|
|
|
|
|
|
|
|
if not as_groups:
|
|
|
|
|
if not vpc_zone_identifier and not availability_zones:
|
|
|
|
|
availability_zones = module.params['availability_zones'] = [zone.name for zone in ec2_connection.get_all_zones()]
|
|
|
|
|
enforce_required_arguments(module)
|
|
|
|
|
launch_configs = connection.get_all_launch_configurations(names=[launch_config_name])
|
|
|
|
|
ag = AutoScalingGroup(
|
|
|
|
|
group_name=group_name,
|
|
|
|
|
load_balancers=load_balancers,
|
|
|
|
@ -143,31 +172,48 @@ def create_autoscaling_group(connection, module):
|
|
|
|
|
max_size=max_size,
|
|
|
|
|
desired_capacity=desired_capacity,
|
|
|
|
|
vpc_zone_identifier=vpc_zone_identifier,
|
|
|
|
|
connection=connection)
|
|
|
|
|
connection=connection,
|
|
|
|
|
tags=asg_tags)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
connection.create_auto_scaling_group(ag)
|
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
asg_properties = get_properties(ag)
|
|
|
|
|
module.exit_json(changed=True, **asg_properties)
|
|
|
|
|
except BotoServerError, e:
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
else:
|
|
|
|
|
as_group = as_groups[0]
|
|
|
|
|
changed = False
|
|
|
|
|
for attr in ('launch_config_name', 'max_size', 'min_size', 'desired_capacity',
|
|
|
|
|
'vpc_zone_identifier', 'availability_zones'):
|
|
|
|
|
if getattr(as_group, attr) != module.params.get(attr):
|
|
|
|
|
for attr in ASG_ATTRIBUTES:
|
|
|
|
|
if module.params.get(attr) and getattr(as_group, attr) != module.params.get(attr):
|
|
|
|
|
changed = True
|
|
|
|
|
setattr(as_group, attr, module.params.get(attr))
|
|
|
|
|
|
|
|
|
|
if len(set_tags) > 0:
|
|
|
|
|
existing_tags = as_group.tags
|
|
|
|
|
existing_tag_map = dict((tag.key, tag) for tag in existing_tags)
|
|
|
|
|
for tag in set_tags:
|
|
|
|
|
if ( not tag['key'] in existing_tag_map or
|
|
|
|
|
existing_tag_map[tag['key']].value != tag['value'] or
|
|
|
|
|
('propagate_at_launch' in tag and
|
|
|
|
|
existing_tag_map[tag['key']].propagate_at_launch != tag['propagate_at_launch']) ):
|
|
|
|
|
|
|
|
|
|
changed = True
|
|
|
|
|
continue
|
|
|
|
|
if changed:
|
|
|
|
|
connection.create_or_update_tags(asg_tags)
|
|
|
|
|
|
|
|
|
|
# handle loadbalancers separately because None != []
|
|
|
|
|
load_balancers = module.params.get('load_balancers') or []
|
|
|
|
|
if as_group.load_balancers != load_balancers:
|
|
|
|
|
if load_balancers and as_group.load_balancers != load_balancers:
|
|
|
|
|
changed = True
|
|
|
|
|
as_group.load_balancers = module.params.get('load_balancers')
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if changed:
|
|
|
|
|
as_group.update()
|
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
|
asg_properties = get_properties(as_group)
|
|
|
|
|
module.exit_json(changed=changed, **asg_properties)
|
|
|
|
|
except BotoServerError, e:
|
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
@ -207,6 +253,7 @@ def main():
|
|
|
|
|
desired_capacity=dict(type='int'),
|
|
|
|
|
vpc_zone_identifier=dict(type='str'),
|
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
|
tags=dict(type='list', default=[]),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec)
|
|
|
|
|