ec2_asg module enhancements

- Added a more verbose response
  - includes its settable attributes and a list of its instances.
  - allows setting of tags, changes upon which mark the task changed
  - allow getting of information from asg module, not just setting
  - doesn't mark changed if the parameter wasn't specified
  - Availability Zones are pulled from the region
reviewable/pr18780/r1
Matt Ferrante 11 years ago
parent 9d86d41741
commit 05632e17c9

@ -67,6 +67,11 @@ options:
- List of VPC subnets to use - List of VPC subnets to use
required: false required: false
default: None 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
extends_documentation_fragment: aws extends_documentation_fragment: aws
""" """
@ -80,6 +85,11 @@ EXAMPLES = '''
max_size: 10 max_size: 10
desired_capacity: 5 desired_capacity: 5
vpc_zone_identifier: 'subnet-abcd1234,subnet-1a2b3c4d' vpc_zone_identifier: 'subnet-abcd1234,subnet-1a2b3c4d'
tags:
- key: environment
value: production
propagate_at_launch: no
''' '''
import sys import sys
@ -90,12 +100,14 @@ from ansible.module_utils.ec2 import *
try: try:
import boto.ec2.autoscale import boto.ec2.autoscale
from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup, Tag
from boto.exception import BotoServerError from boto.exception import BotoServerError
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" print "failed=True msg='boto required for this module'"
sys.exit(1) sys.exit(1)
ASG_ATTRIBUTES = ('launch_config_name', 'max_size', 'min_size', 'desired_capacity',
'vpc_zone_identifier', 'availability_zones')
def enforce_required_arguments(module): def enforce_required_arguments(module):
''' As many arguments are not required for autoscale group deletion ''' As many arguments are not required for autoscale group deletion
@ -109,8 +121,15 @@ def enforce_required_arguments(module):
module.fail_json(msg="Missing required arguments for autoscaling group create/update: %s" % ",".join(missing_args)) 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): def create_autoscaling_group(connection, module):
enforce_required_arguments(module)
group_name = module.params.get('name') group_name = module.params.get('name')
load_balancers = module.params['load_balancers'] load_balancers = module.params['load_balancers']
@ -120,8 +139,7 @@ def create_autoscaling_group(connection, module):
max_size = module.params['max_size'] max_size = module.params['max_size']
desired_capacity = module.params.get('desired_capacity') desired_capacity = module.params.get('desired_capacity')
vpc_zone_identifier = module.params.get('vpc_zone_identifier') vpc_zone_identifier = module.params.get('vpc_zone_identifier')
set_tags = module.params.get('tags')
launch_configs = connection.get_all_launch_configurations(names=[launch_config_name])
as_groups = connection.get_all_groups(names=[group_name]) as_groups = connection.get_all_groups(names=[group_name])
@ -131,9 +149,19 @@ def create_autoscaling_group(connection, module):
ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params) ec2_connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
except boto.exception.NoAuthHandlerFound, e: except boto.exception.NoAuthHandlerFound, e:
module.fail_json(msg=str(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 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( ag = AutoScalingGroup(
group_name=group_name, group_name=group_name,
load_balancers=load_balancers, load_balancers=load_balancers,
@ -143,31 +171,48 @@ def create_autoscaling_group(connection, module):
max_size=max_size, max_size=max_size,
desired_capacity=desired_capacity, desired_capacity=desired_capacity,
vpc_zone_identifier=vpc_zone_identifier, vpc_zone_identifier=vpc_zone_identifier,
connection=connection) connection=connection,
tags=asg_tags)
try: try:
connection.create_auto_scaling_group(ag) 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: except BotoServerError, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
else: else:
as_group = as_groups[0] as_group = as_groups[0]
changed = False changed = False
for attr in ('launch_config_name', 'max_size', 'min_size', 'desired_capacity', for attr in ASG_ATTRIBUTES:
'vpc_zone_identifier', 'availability_zones'): if module.params.get(attr) and getattr(as_group, attr) != module.params.get(attr):
if getattr(as_group, attr) != module.params.get(attr):
changed = True changed = True
setattr(as_group, attr, module.params.get(attr)) 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 != [] # handle loadbalancers separately because None != []
load_balancers = module.params.get('load_balancers') or [] 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 changed = True
as_group.load_balancers = module.params.get('load_balancers') as_group.load_balancers = module.params.get('load_balancers')
try: try:
if changed: if changed:
as_group.update() as_group.update()
module.exit_json(changed=changed) asg_properties = get_properties(as_group)
module.exit_json(changed=changed, **asg_properties)
except BotoServerError, e: except BotoServerError, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
@ -207,6 +252,7 @@ def main():
desired_capacity=dict(type='int'), desired_capacity=dict(type='int'),
vpc_zone_identifier=dict(type='str'), vpc_zone_identifier=dict(type='str'),
state=dict(default='present', choices=['present', 'absent']), state=dict(default='present', choices=['present', 'absent']),
tags=dict(type='list', default=[]),
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)

Loading…
Cancel
Save