|
|
@ -1,18 +1,10 @@ |
|
|
|
#!/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/>. |
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function |
|
|
|
__metaclass__ = type |
|
|
|
|
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1', |
|
|
|
'status': ['stableinterface'], |
|
|
|
'supported_by': 'community'} |
|
|
@ -21,119 +13,150 @@ ANSIBLE_METADATA = {'metadata_version': '1.1', |
|
|
|
DOCUMENTATION = """ |
|
|
|
--- |
|
|
|
module: ec2_asg |
|
|
|
short_description: Create or delete AWS Autoscaling Groups |
|
|
|
short_description: Create or delete AWS AutoScaling Groups (ASGs) |
|
|
|
description: |
|
|
|
- Can create or delete AWS Autoscaling Groups |
|
|
|
- Can be used with the ec2_lc module to manage Launch Configurations |
|
|
|
- Can create or delete AWS AutoScaling Groups. |
|
|
|
- Can be used with the M(ec2_lc) module to manage Launch Configurations. |
|
|
|
version_added: "1.6" |
|
|
|
author: "Gareth Rushgrove (@garethr)" |
|
|
|
requirements: [ "boto3", "botocore" ] |
|
|
|
options: |
|
|
|
state: |
|
|
|
description: |
|
|
|
- register or deregister the instance |
|
|
|
- Register or deregister the instance. |
|
|
|
choices: ['present', 'absent'] |
|
|
|
default: present |
|
|
|
type: str |
|
|
|
name: |
|
|
|
description: |
|
|
|
- Unique name for group to be created or deleted |
|
|
|
- Unique name for group to be created or deleted. |
|
|
|
required: true |
|
|
|
type: str |
|
|
|
load_balancers: |
|
|
|
description: |
|
|
|
- List of ELB names to use for the group. Use for classic load balancers. |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
target_group_arns: |
|
|
|
description: |
|
|
|
- List of target group ARNs to use for the group. Use for application load balancers. |
|
|
|
version_added: "2.4" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
availability_zones: |
|
|
|
description: |
|
|
|
- List of availability zone names in which to create the group. Defaults to all the availability zones in the region if vpc_zone_identifier is not set. |
|
|
|
- List of availability zone names in which to create the group. |
|
|
|
- Defaults to all the availability zones in the region if I(vpc_zone_identifier) is not set. |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
launch_config_name: |
|
|
|
description: |
|
|
|
- Name of the Launch configuration to use for the group. See the ec2_lc module for managing these. |
|
|
|
If unspecified then the current group value will be used. One of launch_config_name or launch_template must be provided. |
|
|
|
- Name of the Launch configuration to use for the group. See the M(ec2_lc) module for managing these. |
|
|
|
- If unspecified then the current group value will be used. One of I(launch_config_name) or I(launch_template) must be provided. |
|
|
|
type: str |
|
|
|
launch_template: |
|
|
|
description: |
|
|
|
- Dictionary describing the Launch Template to use |
|
|
|
suboptions: |
|
|
|
version: |
|
|
|
description: |
|
|
|
- The version number of the launch template to use. Defaults to latest version if not provided. |
|
|
|
default: "latest" |
|
|
|
- The version number of the launch template to use. |
|
|
|
- Defaults to latest version if not provided. |
|
|
|
type: str |
|
|
|
launch_template_name: |
|
|
|
description: |
|
|
|
- The name of the launch template. Only one of launch_template_name or launch_template_id is required. |
|
|
|
- The name of the launch template. Only one of I(launch_template_name) or I(launch_template_id) is required. |
|
|
|
type: str |
|
|
|
launch_template_id: |
|
|
|
description: |
|
|
|
- The id of the launch template. Only one of launch_template_name or launch_template_id is required. |
|
|
|
- The id of the launch template. Only one of I(launch_template_name) or I(launch_template_id) is required. |
|
|
|
type: str |
|
|
|
type: dict |
|
|
|
version_added: "2.8" |
|
|
|
min_size: |
|
|
|
description: |
|
|
|
- Minimum number of instances in group, if unspecified then the current group value will be used. |
|
|
|
type: int |
|
|
|
max_size: |
|
|
|
description: |
|
|
|
- Maximum number of instances in group, if unspecified then the current group value will be used. |
|
|
|
type: int |
|
|
|
mixed_instances_policy: |
|
|
|
description: |
|
|
|
- Using mixed intances policy while ASG present |
|
|
|
required: false |
|
|
|
version_added: "2.9" |
|
|
|
version_added: "2.8" |
|
|
|
suboptions: |
|
|
|
instance_types: |
|
|
|
description: |
|
|
|
- A list of instance_types. |
|
|
|
type: list |
|
|
|
type: dict |
|
|
|
placement_group: |
|
|
|
description: |
|
|
|
- Physical location of your cluster placement group created in Amazon EC2. |
|
|
|
version_added: "2.3" |
|
|
|
type: str |
|
|
|
desired_capacity: |
|
|
|
description: |
|
|
|
- Desired number of instances in group, if unspecified then the current group value will be used. |
|
|
|
type: int |
|
|
|
replace_all_instances: |
|
|
|
description: |
|
|
|
- In a rolling fashion, replace all instances that used the old launch configuration with one from the new launch configuration. |
|
|
|
It increases the ASG size by C(replace_batch_size), waits for the new instances to be up and running. |
|
|
|
It increases the ASG size by I(replace_batch_size), waits for the new instances to be up and running. |
|
|
|
After that, it terminates a batch of old instances, waits for the replacements, and repeats, until all old instances are replaced. |
|
|
|
Once that's done the ASG size is reduced back to the expected size. |
|
|
|
version_added: "1.8" |
|
|
|
default: 'no' |
|
|
|
default: false |
|
|
|
type: bool |
|
|
|
replace_batch_size: |
|
|
|
description: |
|
|
|
- Number of instances you'd like to replace at a time. Used with replace_all_instances. |
|
|
|
- Number of instances you'd like to replace at a time. Used with I(replace_all_instances). |
|
|
|
required: false |
|
|
|
version_added: "1.8" |
|
|
|
default: 1 |
|
|
|
type: int |
|
|
|
replace_instances: |
|
|
|
description: |
|
|
|
- List of instance_ids belonging to the named ASG that you would like to terminate and be replaced with instances matching the current launch |
|
|
|
configuration. |
|
|
|
- List of I(instance_ids) belonging to the named AutoScalingGroup that you would like to terminate and be replaced with instances |
|
|
|
matching the current launch configuration. |
|
|
|
version_added: "1.8" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
lc_check: |
|
|
|
description: |
|
|
|
- Check to make sure instances that are being replaced with replace_instances do not already have the current launch_config. |
|
|
|
- Check to make sure instances that are being replaced with I(replace_instances) do not already have the current I(launch_config). |
|
|
|
version_added: "1.8" |
|
|
|
default: 'yes' |
|
|
|
default: true |
|
|
|
type: bool |
|
|
|
lt_check: |
|
|
|
description: |
|
|
|
- Check to make sure instances that are being replaced with replace_instances do not already have the current launch_template or launch_template version. |
|
|
|
- Check to make sure instances that are being replaced with I(replace_instances) do not already have the current |
|
|
|
I(launch_template or I(launch_template) I(version). |
|
|
|
version_added: "2.8" |
|
|
|
default: 'yes' |
|
|
|
default: true |
|
|
|
type: bool |
|
|
|
vpc_zone_identifier: |
|
|
|
description: |
|
|
|
- List of VPC subnets to use |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
tags: |
|
|
|
description: |
|
|
|
- A list of tags to add to the Auto Scale Group. Optional key is 'propagate_at_launch', which defaults to true. |
|
|
|
- A list of tags to add to the Auto Scale Group. |
|
|
|
- Optional key is I(propagate_at_launch), which defaults to true. |
|
|
|
- When I(propagate_at_launch) is true the tags will be propagated to the Instances created. |
|
|
|
version_added: "1.7" |
|
|
|
type: list |
|
|
|
elements: dict |
|
|
|
health_check_period: |
|
|
|
description: |
|
|
|
- Length of time in seconds after a new EC2 instance comes into service that Auto Scaling starts checking its health. |
|
|
|
required: false |
|
|
|
default: 300 seconds |
|
|
|
default: 300 |
|
|
|
version_added: "1.7" |
|
|
|
type: int |
|
|
|
health_check_type: |
|
|
|
description: |
|
|
|
- The service you want the health status from, Amazon EC2 or Elastic Load Balancer. |
|
|
@ -141,76 +164,94 @@ options: |
|
|
|
default: EC2 |
|
|
|
version_added: "1.7" |
|
|
|
choices: ['EC2', 'ELB'] |
|
|
|
type: str |
|
|
|
default_cooldown: |
|
|
|
description: |
|
|
|
- The number of seconds after a scaling activity completes before another can begin. |
|
|
|
default: 300 seconds |
|
|
|
default: 300 |
|
|
|
version_added: "2.0" |
|
|
|
type: int |
|
|
|
wait_timeout: |
|
|
|
description: |
|
|
|
- How long to wait for instances to become viable when replaced. If you experience the error "Waited too long for ELB instances to be healthy", |
|
|
|
try increasing this value. |
|
|
|
default: 300 |
|
|
|
type: int |
|
|
|
version_added: "1.8" |
|
|
|
wait_for_instances: |
|
|
|
description: |
|
|
|
- Wait for the ASG instances to be in a ready state before exiting. If instances are behind an ELB, it will wait until the ELB determines all |
|
|
|
instances have a lifecycle_state of "InService" and a health_status of "Healthy". |
|
|
|
version_added: "1.9" |
|
|
|
default: 'yes' |
|
|
|
default: true |
|
|
|
type: bool |
|
|
|
termination_policies: |
|
|
|
description: |
|
|
|
- An ordered list of criteria used for selecting instances to be removed from the Auto Scaling group when reducing capacity. |
|
|
|
- For 'Default', when used to create a new autoscaling group, the "Default"i value is used. When used to change an existent autoscaling group, the |
|
|
|
current termination policies are maintained. |
|
|
|
- Using I(termination_policies=Default) when modifying an existing AutoScalingGroup will result in the existing policy being retained |
|
|
|
instead of changed to C(Default). |
|
|
|
- 'Valid values include: C(Default), C(OldestInstance), C(NewestInstance), C(OldestLaunchConfiguration), C(ClosestToNextInstanceHour)' |
|
|
|
- 'Full documentation of valid values can be found in the AWS documentation:' |
|
|
|
- 'U(https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html#custom-termination-policy)' |
|
|
|
default: Default |
|
|
|
choices: ['OldestInstance', 'NewestInstance', 'OldestLaunchConfiguration', 'ClosestToNextInstanceHour', 'Default'] |
|
|
|
version_added: "2.0" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
notification_topic: |
|
|
|
description: |
|
|
|
- A SNS topic ARN to send auto scaling notifications to. |
|
|
|
version_added: "2.2" |
|
|
|
type: str |
|
|
|
notification_types: |
|
|
|
description: |
|
|
|
- A list of auto scaling events to trigger notifications on. |
|
|
|
default: |
|
|
|
- 'autoscaling:EC2_INSTANCE_LAUNCH' |
|
|
|
- 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR' |
|
|
|
- 'autoscaling:EC2_INSTANCE_TERMINATE' |
|
|
|
- 'autoscaling:EC2_INSTANCE_TERMINATE_ERROR' |
|
|
|
- 'autoscaling:EC2_INSTANCE_LAUNCH' |
|
|
|
- 'autoscaling:EC2_INSTANCE_LAUNCH_ERROR' |
|
|
|
- 'autoscaling:EC2_INSTANCE_TERMINATE' |
|
|
|
- 'autoscaling:EC2_INSTANCE_TERMINATE_ERROR' |
|
|
|
required: false |
|
|
|
version_added: "2.2" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
suspend_processes: |
|
|
|
description: |
|
|
|
- A list of scaling processes to suspend. |
|
|
|
- 'Valid values include:' |
|
|
|
- C(Launch), C(Terminate), C(HealthCheck), C(ReplaceUnhealthy), C(AZRebalance), C(AlarmNotification), C(ScheduledActions), C(AddToLoadBalancer) |
|
|
|
- 'Full documentation of valid values can be found in the AWS documentation:' |
|
|
|
- 'U(https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html)' |
|
|
|
default: [] |
|
|
|
choices: ['Launch', 'Terminate', 'HealthCheck', 'ReplaceUnhealthy', 'AZRebalance', 'AlarmNotification', 'ScheduledActions', 'AddToLoadBalancer'] |
|
|
|
version_added: "2.3" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
metrics_collection: |
|
|
|
description: |
|
|
|
- Enable ASG metrics collection |
|
|
|
- Enable ASG metrics collection. |
|
|
|
type: bool |
|
|
|
default: 'no' |
|
|
|
default: false |
|
|
|
version_added: "2.6" |
|
|
|
metrics_granularity: |
|
|
|
description: |
|
|
|
- When metrics_collection is enabled this will determine granularity of metrics collected by CloudWatch |
|
|
|
default: "1minute" |
|
|
|
- When I(metrics_collection=true) this will determine the granularity of metrics collected by CloudWatch. |
|
|
|
default: "1Minute" |
|
|
|
version_added: "2.6" |
|
|
|
type: str |
|
|
|
metrics_list: |
|
|
|
description: |
|
|
|
- List of autoscaling metrics to collect when enabling metrics_collection |
|
|
|
- List of autoscaling metrics to collect when I(metrics_collection=true). |
|
|
|
default: |
|
|
|
- 'GroupMinSize' |
|
|
|
- 'GroupMaxSize' |
|
|
|
- 'GroupDesiredCapacity' |
|
|
|
- 'GroupInServiceInstances' |
|
|
|
- 'GroupPendingInstances' |
|
|
|
- 'GroupStandbyInstances' |
|
|
|
- 'GroupTerminatingInstances' |
|
|
|
- 'GroupTotalInstances' |
|
|
|
- 'GroupMinSize' |
|
|
|
- 'GroupMaxSize' |
|
|
|
- 'GroupDesiredCapacity' |
|
|
|
- 'GroupInServiceInstances' |
|
|
|
- 'GroupPendingInstances' |
|
|
|
- 'GroupStandbyInstances' |
|
|
|
- 'GroupTerminatingInstances' |
|
|
|
- 'GroupTotalInstances' |
|
|
|
version_added: "2.6" |
|
|
|
type: list |
|
|
|
elements: str |
|
|
|
extends_documentation_fragment: |
|
|
|
- aws |
|
|
|
- ec2 |
|
|
@ -396,6 +437,11 @@ min_size: |
|
|
|
returned: success |
|
|
|
type: int |
|
|
|
sample: 1 |
|
|
|
mixed_instance_policy: |
|
|
|
description: Returns the list of used mixed instance policy if set. |
|
|
|
returned: success |
|
|
|
type: list |
|
|
|
sample: ["t3.micro", "t3a.micro"] |
|
|
|
pending_instances: |
|
|
|
description: Number of instances in pending state |
|
|
|
returned: success |
|
|
@ -681,7 +727,15 @@ def get_properties(autoscaling_group): |
|
|
|
properties['termination_policies'] = autoscaling_group.get('TerminationPolicies') |
|
|
|
properties['target_group_arns'] = autoscaling_group.get('TargetGroupARNs') |
|
|
|
properties['vpc_zone_identifier'] = autoscaling_group.get('VPCZoneIdentifier') |
|
|
|
properties['metrics_collection'] = autoscaling_group.get('EnabledMetrics') |
|
|
|
raw_mixed_instance_object = autoscaling_group.get('MixedInstancesPolicy') |
|
|
|
if raw_mixed_instance_object: |
|
|
|
raw_mixed_instance_object.get('LaunchTemplate').get('Overrides') |
|
|
|
properties['mixed_instances_policy'] = [x['InstanceType'] for x in raw_mixed_instance_object.get('LaunchTemplate').get('Overrides')] |
|
|
|
|
|
|
|
metrics = autoscaling_group.get('EnabledMetrics') |
|
|
|
if metrics: |
|
|
|
metrics.sort(key=lambda x: x["Metric"]) |
|
|
|
properties['metrics_collection'] = metrics |
|
|
|
|
|
|
|
if properties['target_group_arns']: |
|
|
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) |
|
|
@ -724,6 +778,7 @@ def get_launch_object(connection, ec2_connection): |
|
|
|
launch_object = {"LaunchTemplate": {"LaunchTemplateId": lt['LaunchTemplateId'], "Version": launch_template['version']}} |
|
|
|
else: |
|
|
|
launch_object = {"LaunchTemplate": {"LaunchTemplateId": lt['LaunchTemplateId'], "Version": str(lt['LatestVersionNumber'])}} |
|
|
|
|
|
|
|
if mixed_instances_policy: |
|
|
|
instance_types = mixed_instances_policy.get('instance_types', []) |
|
|
|
policy = { |
|
|
@ -1059,6 +1114,10 @@ def create_autoscaling_group(connection): |
|
|
|
if len(set_tags) > 0: |
|
|
|
have_tags = as_group.get('Tags') |
|
|
|
want_tags = asg_tags |
|
|
|
if have_tags: |
|
|
|
have_tags.sort(key=lambda x: x["Key"]) |
|
|
|
if want_tags: |
|
|
|
want_tags.sort(key=lambda x: x["Key"]) |
|
|
|
dead_tags = [] |
|
|
|
have_tag_keyvals = [x['Key'] for x in have_tags] |
|
|
|
want_tag_keyvals = [x['Key'] for x in want_tags] |
|
|
@ -1182,9 +1241,6 @@ def create_autoscaling_group(connection): |
|
|
|
if 'LaunchConfigurationName' in launch_object: |
|
|
|
ag['LaunchConfigurationName'] = launch_object['LaunchConfigurationName'] |
|
|
|
elif 'LaunchTemplate' in launch_object: |
|
|
|
import q |
|
|
|
import json |
|
|
|
q(launch_object) |
|
|
|
if 'MixedInstancesPolicy' in launch_object: |
|
|
|
ag['MixedInstancesPolicy'] = launch_object['MixedInstancesPolicy'] |
|
|
|
else: |
|
|
@ -1436,7 +1492,7 @@ def get_instances_by_launch_config(props, lc_check, initial_instances): |
|
|
|
def get_instances_by_launch_template(props, lt_check, initial_instances): |
|
|
|
new_instances = [] |
|
|
|
old_instances = [] |
|
|
|
# old instances are those that have the old launch template or version of the same launch templatec |
|
|
|
# old instances are those that have the old launch template or version of the same launch template |
|
|
|
if lt_check: |
|
|
|
for i in props['instances']: |
|
|
|
# Check if migrating from launch_config_name to launch_template_name first |
|
|
|