ansible.builtin.ec2_asg (v2.6.10) — module

Create or delete AWS Autoscaling Groups

| "added in version" 1.6 of ansible.builtin"

Authors: Gareth Rushgrove (@garethr)

stableinterface | supported by certified

Install Ansible via pip

Install with pip install ansible==2.6.10

Description

Can create or delete AWS Autoscaling Groups

Works with the ec2_lc module to manage Launch Configurations


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Basic configuration

- ec2_asg:
    name: special
    load_balancers: [ 'lb1', 'lb2' ]
    availability_zones: [ 'eu-west-1a', 'eu-west-1b' ]
    launch_config_name: 'lc-1'
    min_size: 1
    max_size: 10
    desired_capacity: 5
    vpc_zone_identifier: [ 'subnet-abcd1234', 'subnet-1a2b3c4d' ]
    tags:
      - environment: production
        propagate_at_launch: no
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Rolling ASG Updates

# Below is an example of how to assign a new launch config to an ASG and terminate old instances.
#
# All instances in "myasg" that do not have the launch configuration named "my_new_lc" will be terminated in
# a rolling fashion with instances using the current launch configuration, "my_new_lc".
#
# This could also be considered a rolling deploy of a pre-baked AMI.
#
# If this is a newly created group, the instances will not be replaced since all instances
# will have the current launch configuration.

- name: create launch config
  ec2_lc:
    name: my_new_lc
    image_id: ami-lkajsf
    key_name: mykey
    region: us-east-1
    security_groups: sg-23423
    instance_type: m1.small
    assign_public_ip: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- ec2_asg:
    name: myasg
    launch_config_name: my_new_lc
    health_check_period: 60
    health_check_type: ELB
    replace_all_instances: yes
    min_size: 5
    max_size: 5
    desired_capacity: 5
    region: us-east-1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# To only replace a couple of instances instead of all of them, supply a list
# to "replace_instances":

- ec2_asg:
    name: myasg
    launch_config_name: my_new_lc
    health_check_period: 60
    health_check_type: ELB
    replace_instances:
    - i-b345231
    - i-24c2931
    min_size: 5
    max_size: 5
    desired_capacity: 5
    region: us-east-1

Inputs

    
name:
    description:
    - Unique name for group to be created or deleted
    required: true

tags:
    description:
    - A list of tags to add to the Auto Scale Group. Optional key is 'propagate_at_launch',
      which defaults to true.
    version_added: '1.7'
    version_added_collection: ansible.builtin

state:
    choices:
    - present
    - absent
    default: present
    description:
    - register or deregister the instance

region:
    aliases:
    - aws_region
    - ec2_region
    description:
    - The AWS region to use.
    - For global services such as IAM, Route53 and CloudFront, I(region) is ignored.
    - The C(AWS_REGION) or C(EC2_REGION) environment variables may also be used.
    - See the Amazon AWS documentation for more information U(http://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region).
    - The C(ec2_region) alias has been deprecated and will be removed in a release after
      2024-12-01
    - Support for the C(EC2_REGION) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

profile:
    aliases:
    - aws_profile
    description:
    - A named AWS profile to use for authentication.
    - See the AWS documentation for more information about named profiles U(https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html).
    - The C(AWS_PROFILE) environment variable may also be used.  Prior to release 6.0.0
      the C(AWS_PROFILE) environment variable will be ignored if any of I(access_key),
      I(secret_key), or I(session_token) are passed.  After release 6.0.0 I(profile) will
      always fall back to the C(AWS_PROFILE) environment variable if set.
    - The I(profile) option is mutually exclusive with the I(aws_access_key), I(aws_secret_key)
      and I(security_token) options.
    type: str

lc_check:
    default: 'yes'
    description:
    - Check to make sure instances that are being replaced with replace_instances do not
      already have the current launch_config.
    version_added: '1.8'
    version_added_collection: ansible.builtin

max_size:
    description:
    - Maximum number of instances in group, if unspecified then the current group value
      will be used.

min_size:
    description:
    - Minimum number of instances in group, if unspecified then the current group value
      will be used.

access_key:
    aliases:
    - aws_access_key_id
    - aws_access_key
    - ec2_access_key
    description:
    - AWS access key ID.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_ACCESS_KEY_ID), C(AWS_ACCESS_KEY) or C(EC2_ACCESS_KEY) environment variables
      may also be used in decreasing order of preference.  Prior to release 6.0.0 these
      environment variables will be ignored if the I(profile) parameter is passed.  After
      release 6.0.0 I(access_key) will always fall back to the environment variables if
      set.
    - The I(aws_access_key) and I(profile) options are mutually exclusive.
    - The I(aws_access_key_id) alias was added in release 5.1.0 for consistency with the
      AWS botocore SDK.
    - The I(ec2_access_key) alias has been deprecated and will be removed in a release
      after 2024-12-01.
    - Support for the C(EC2_ACCESS_KEY) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

aws_config:
    description:
    - A dictionary to modify the botocore configuration.
    - Parameters can be found in the AWS documentation U(https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html#botocore.config.Config).
    type: dict

secret_key:
    aliases:
    - aws_secret_access_key
    - aws_secret_key
    - ec2_secret_key
    description:
    - AWS secret access key.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_SECRET_ACCESS_KEY), C(AWS_SECRET_KEY), or C(EC2_SECRET_KEY) environment
      variables may also be used in decreasing order of preference.  Prior to release
      6.0.0 these environment variables will be ignored if the I(profile) parameter is
      passed.  After release 6.0.0 I(secret_key) will always fall back to the environment
      variables if set.
    - The I(secret_key) and I(profile) options are mutually exclusive.
    - The I(aws_secret_access_key) alias was added in release 5.1.0 for consistency with
      the AWS botocore SDK.
    - The I(ec2_secret_key) alias has been deprecated and will be removed in a release
      after 2024-12-01.
    - Support for the C(EC2_SECRET_KEY) environment variable has been deprecated and will
      be removed in a release after 2024-12-01.
    type: str

endpoint_url:
    aliases:
    - ec2_url
    - aws_endpoint_url
    - s3_url
    description:
    - URL to connect to instead of the default AWS endpoints.  While this can be used
      to connection to other AWS-compatible services the amazon.aws and community.aws
      collections are only tested against AWS.
    - The  C(AWS_URL) or C(EC2_URL) environment variables may also be used, in decreasing
      order of preference.
    - The I(ec2_url) and I(s3_url) aliases have been deprecated and will be removed in
      a release after 2024-12-01.
    - Support for the C(EC2_URL) environment variable has been deprecated and will be
      removed in a release after 2024-12-01.
    type: str

metrics_list:
    default:
    - GroupMinSize
    - GroupMaxSize
    - GroupDesiredCapacity
    - GroupInServiceInstances
    - GroupPendingInstances
    - GroupStandbyInstances
    - GroupTerminatingInstances
    - GroupTotalInstances
    description:
    - List of autoscaling metrics to collect when enabling metrics_collection
    version_added: '2.5'
    version_added_collection: ansible.builtin

wait_timeout:
    default: 300
    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.
    version_added: '1.8'
    version_added_collection: ansible.builtin

aws_ca_bundle:
    description:
    - The location of a CA Bundle to use when validating SSL certificates.
    - The C(AWS_CA_BUNDLE) environment variable may also be used.
    type: path

session_token:
    aliases:
    - aws_session_token
    - security_token
    - aws_security_token
    - access_token
    description:
    - AWS STS session token for use with temporary credentials.
    - See the AWS documentation for more information about access tokens U(https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys).
    - The C(AWS_SESSION_TOKEN), C(AWS_SECURITY_TOKEN) or C(EC2_SECURITY_TOKEN) environment
      variables may also be used in decreasing order of preference. Prior to release 6.0.0
      these environment variables will be ignored if the I(profile) parameter is passed.  After
      release 6.0.0 I(session_token) will always fall back to the environment variables
      if set.
    - The I(security_token) and I(profile) options are mutually exclusive.
    - Aliases I(aws_session_token) and I(session_token) were added in release 3.2.0, with
      the parameter being renamed from I(security_token) to I(session_token) in release
      6.0.0.
    - The I(security_token), I(aws_security_token), and I(access_token) aliases have been
      deprecated and will be removed in a release after 2024-12-01.
    - Support for the C(EC2_SECRET_KEY) and C(AWS_SECURITY_TOKEN) environment variables
      has been deprecated and will be removed in a release after 2024-12-01.
    type: str

load_balancers:
    description:
    - List of ELB names to use for the group. Use for classic load balancers.

validate_certs:
    default: true
    description:
    - When set to C(false), SSL certificates will not be validated for communication with
      the AWS APIs.
    - Setting I(validate_certs=false) is strongly discouraged, as an alternative, consider
      setting I(aws_ca_bundle) instead.
    type: bool

placement_group:
    description:
    - Physical location of your cluster placement group created in Amazon EC2.
    version_added: '2.3'
    version_added_collection: ansible.builtin

default_cooldown:
    default: 300 seconds
    description:
    - The number of seconds after a scaling activity completes before another can begin.
    version_added: '2.0'
    version_added_collection: ansible.builtin

desired_capacity:
    description:
    - Desired number of instances in group, if unspecified then the current group value
      will be used.

health_check_type:
    choices:
    - EC2
    - ELB
    default: EC2
    description:
    - The service you want the health status from, Amazon EC2 or Elastic Load Balancer.
    required: false
    version_added: '1.7'
    version_added_collection: ansible.builtin

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.
    version_added: '1.8'
    version_added_collection: ansible.builtin

suspend_processes:
    choices:
    - Launch
    - Terminate
    - HealthCheck
    - ReplaceUnhealthy
    - AZRebalance
    - AlarmNotification
    - ScheduledActions
    - AddToLoadBalancer
    default: []
    description:
    - A list of scaling processes to suspend.
    version_added: '2.3'
    version_added_collection: ansible.builtin

target_group_arns:
    description:
    - List of target group ARNs to use for the group. Use for application load balancers.
    version_added: '2.4'
    version_added_collection: ansible.builtin

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.

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.
    required: true

metrics_collection:
    default: 'no'
    description:
    - Enable ASG metrics collection
    type: bool
    version_added: '2.5'
    version_added_collection: ansible.builtin

notification_topic:
    description:
    - A SNS topic ARN to send auto scaling notifications to.
    version_added: '2.2'
    version_added_collection: ansible.builtin

notification_types:
    default:
    - autoscaling:EC2_INSTANCE_LAUNCH
    - autoscaling:EC2_INSTANCE_LAUNCH_ERROR
    - autoscaling:EC2_INSTANCE_TERMINATE
    - autoscaling:EC2_INSTANCE_TERMINATE_ERROR
    description:
    - A list of auto scaling events to trigger notifications on.
    required: false
    version_added: '2.2'
    version_added_collection: ansible.builtin

replace_batch_size:
    default: 1
    description:
    - Number of instances you'd like to replace at a time.  Used with replace_all_instances.
    required: false
    version_added: '1.8'
    version_added_collection: ansible.builtin

wait_for_instances:
    default: 'yes'
    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'
    version_added_collection: ansible.builtin

health_check_period:
    default: 500 seconds
    description:
    - Length of time in seconds after a new EC2 instance comes into service that Auto
      Scaling starts checking its health.
    required: false
    version_added: '1.7'
    version_added_collection: ansible.builtin

metrics_granularity:
    default: 1minute
    description:
    - When metrics_collection is enabled this will determine granularity of metrics collected
      by CloudWatch
    version_added: '2.5'
    version_added_collection: ansible.builtin

vpc_zone_identifier:
    description:
    - List of VPC subnets to use

termination_policies:
    choices:
    - OldestInstance
    - NewestInstance
    - OldestLaunchConfiguration
    - ClosestToNextInstanceHour
    - Default
    default: Default
    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.
    version_added: '2.0'
    version_added_collection: ansible.builtin

replace_all_instances:
    default: 'no'
    description:
    - In a rolling fashion, replace all instances with an old launch configuration with
      one from the current launch configuration.
    version_added: '1.8'
    version_added_collection: ansible.builtin

debug_botocore_endpoint_logs:
    default: false
    description:
    - Use a C(botocore.endpoint) logger to parse the unique (rather than total) C("resource:action")
      API calls made during a task, outputing the set to the resource_actions key in the
      task results. Use the C(aws_resource_action) callback to output to total list made
      during a playbook.
    - The C(ANSIBLE_DEBUG_BOTOCORE_LOGS) environment variable may also be used.
    type: bool

Outputs

auto_scaling_group_arn:
  description: The unique ARN of the autoscaling group
  returned: success
  sample: arn:aws:autoscaling:us-east-1:123456789012:autoScalingGroup:6a09ad6d-eeee-1234-b987-ee123ced01ad:autoScalingGroupName/myasg
  type: str
auto_scaling_group_name:
  description: The unique name of the auto scaling group
  returned: success
  sample: myasg
  type: str
availability_zones:
  description: The availability zones for the auto scaling group
  returned: success
  sample:
  - us-east-1d
  type: list
created_time:
  description: Timestamp of create time of the auto scaling group
  returned: success
  sample: '2017-11-08T14:41:48.272000+00:00'
  type: str
default_cooldown:
  description: The default cooldown time in seconds.
  returned: success
  sample: 300
  type: int
desired_capacity:
  description: The number of EC2 instances that should be running in this group.
  returned: success
  sample: 3
  type: int
healthcheck_period:
  description: Length of time in seconds after a new EC2 instance comes into service
    that Auto Scaling starts checking its health.
  returned: success
  sample: 30
  type: int
healthcheck_type:
  description: The service you want the health status from, one of "EC2" or "ELB".
  returned: success
  sample: ELB
  type: str
healthy_instances:
  description: Number of instances in a healthy state
  returned: success
  sample: 5
  type: int
in_service_instances:
  description: Number of instances in service
  returned: success
  sample: 3
  type: int
instance_facts:
  description: Dictionary of EC2 instances and their status as it relates to the ASG.
  returned: success
  sample:
    i-0123456789012:
      health_status: Healthy
      launch_config_name: public-webapp-production-1
      lifecycle_state: InService
  type: dict
instances:
  description: list of instance IDs in the ASG
  returned: success
  sample:
  - i-0123456789012
  type: list
launch_config_name:
  description: 'Name of launch configuration associated with the ASG. Same as launch_configuration_name,
    provided for compatibility with ec2_asg module.

    '
  returned: success
  sample: public-webapp-production-1
  type: str
load_balancers:
  description: List of load balancers names attached to the ASG.
  returned: success
  sample:
  - elb-webapp-prod
  type: list
max_size:
  description: Maximum size of group
  returned: success
  sample: 3
  type: int
metrics_collection:
  description: List of enabled AutosSalingGroup metrics
  returned: success
  sample:
  - Granularity: 1Minute
    Metric: GroupInServiceInstances
  type: list
min_size:
  description: Minimum size of group
  returned: success
  sample: 1
  type: int
pending_instances:
  description: Number of instances in pending state
  returned: success
  sample: 1
  type: int
tags:
  description: List of tags for the ASG, and whether or not each tag propagates to
    instances at launch.
  returned: success
  sample:
  - key: Name
    propagate_at_launch: 'true'
    resource_id: public-webapp-production-1
    resource_type: auto-scaling-group
    value: public-webapp-production-1
  - key: env
    propagate_at_launch: 'true'
    resource_id: public-webapp-production-1
    resource_type: auto-scaling-group
    value: production
  type: list
target_group_arns:
  description: List of ARNs of the target groups that the ASG populates
  returned: success
  sample:
  - arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-host-hello/1a2b3c4d5e6f1a2b
  - arn:aws:elasticloadbalancing:ap-southeast-2:123456789012:targetgroup/target-group-path-world/abcd1234abcd1234
  type: list
target_group_names:
  description: List of names of the target groups that the ASG populates
  returned: success
  sample:
  - target-group-host-hello
  - target-group-path-world
  type: list
termination_policies:
  description: A list of termination policies for the group.
  returned: success
  sample:
  - Default
  type: str
unhealthy_instances:
  description: Number of instances in an unhealthy state
  returned: success
  sample: 0
  type: int
viable_instances:
  description: Number of instances in a viable state
  returned: success
  sample: 1
  type: int
vpc_zone_identifier:
  description: VPC zone ID / subnet id for the auto scaling group
  returned: success
  sample: subnet-a31ef45f
  type: str