Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_mode support to ec2_asg #1033

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1033-ec2_asg-check-mode-support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_asg - add check mode support (https://github.com/ansible-collections/community.aws/pull/1033).
10 changes: 10 additions & 0 deletions plugins/modules/ec2_asg.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,9 @@ def create_autoscaling_group(connection):
ResourceType='auto-scaling-group',
ResourceId=group_name))
if not as_groups:
if module.check_mode:
module.exit_json(changed=True, msg="Would have created AutoScalingGroup if not in check_mode.")

if not vpc_zone_identifier and not availability_zones:
availability_zones = module.params['availability_zones'] = [zone['ZoneName'] for
zone in ec2_connection.describe_availability_zones()['AvailabilityZones']]
Expand Down Expand Up @@ -1206,6 +1209,9 @@ def create_autoscaling_group(connection):
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg="Failed to create Autoscaling Group.")
else:
if module.check_mode:
module.exit_json(changed=True, msg="Would have modified AutoScalingGroup if required if not in check_mode.")

as_group = as_groups[0]
initial_asg_properties = get_properties(as_group)
changed = False
Expand Down Expand Up @@ -1401,6 +1407,8 @@ def delete_autoscaling_group(connection):
del_notification_config(connection, group_name, notification_topic)
groups = describe_autoscaling_groups(connection, group_name)
if groups:
if module.check_mode:
module.exit_json(changed=True, msg="Would have deleted AutoScalingGroup if not in check_mode.")
wait_timeout = time.time() + wait_timeout
if not wait_for_instances:
delete_asg(connection, group_name, force_delete=True)
Expand Down Expand Up @@ -1456,6 +1464,7 @@ def replace(connection):
min_size = module.params.get('min_size')
desired_capacity = module.params.get('desired_capacity')
launch_config_name = module.params.get('launch_config_name')

# Required to maintain the default value being set to 'true'
if launch_config_name:
lc_check = module.params.get('lc_check')
Expand Down Expand Up @@ -1891,6 +1900,7 @@ def main():
global module
module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[
['replace_all_instances', 'replace_instances'],
['replace_all_instances', 'detach_instances'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@
that:
- "output.viable_instances == 1"

- name: Enable metrics collection - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg"
metrics_collection: yes
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:UpdateAutoScalingGroup" not in output.resource_actions'

- name: Enable metrics collection
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand All @@ -70,7 +83,7 @@
that:
- output is changed

- name: Enable metrics collection (check idempotency)
- name: Enable metrics collection (idempotency)
ec2_asg:
name: "{{ resource_prefix }}-asg"
metrics_collection: yes
Expand All @@ -80,6 +93,20 @@
that:
- output is not changed

- name: Disable metrics collection - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg"
metrics_collection: no
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:UpdateAutoScalingGroup" not in output.resource_actions'


- name: Disable metrics collection
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand All @@ -90,7 +117,7 @@
that:
- output is changed

- name: Disable metrics collection (check idempotency)
- name: Disable metrics collection (idempotency)
ec2_asg:
name: "{{ resource_prefix }}-asg"
metrics_collection: no
Expand Down Expand Up @@ -415,6 +442,30 @@
groups:
- "{{ sg.group_id }}"

- name: update autoscaling group with mixed-instances policy with mixed instances types - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_template:
launch_template_name: "{{ resource_prefix }}-lt"
desired_capacity: 1
min_size: 1
max_size: 1
vpc_zone_identifier: "{{ testing_subnet.subnet.id }}"
state: present
mixed_instances_policy:
instance_types:
- t3.micro
- t2.nano
wait_for_instances: yes
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:CreateOrUpdateTags" not in output.resource_actions'

- name: update autoscaling group with mixed-instances policy with mixed instances types
ec2_asg:
name: "{{ resource_prefix }}-asg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@
- '"autoscaling:CreateLaunchConfiguration" in create_lc.resource_actions'

#----------------------------------------------------------------------

- name: create a AutoScalingGroup to be used for instance_detach test - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
launch_config_name: "{{ resource_prefix }}-lc-detach-test"
health_check_period: 60
health_check_type: ELB
replace_all_instances: yes
min_size: 3
max_size: 6
desired_capacity: 3
region: "{{ aws_region }}"
register: create_asg
check_mode: true

- assert:
that:
- create_asg is changed
- create_asg is not failed
- '"autoscaling:CreateAutoScalingGroup" not in create_asg.resource_actions'

- name: create a AutoScalingGroup to be used for instance_detach test
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand Down Expand Up @@ -68,6 +89,28 @@

#----------------------------------------------------------------------

- name: detach 2 instance from the asg and replace with other instances - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
launch_config_name: "{{ resource_prefix }}-lc-detach-test"
health_check_period: 60
health_check_type: ELB
min_size: 3
max_size: 3
desired_capacity: 3
region: "{{ aws_region }}"
detach_instances:
- '{{ init_instance_1 }}'
- '{{ init_instance_2 }}'
register: detach_result
check_mode: true

- assert:
that:
- detach_result is changed
- detach_result is not failed
- '"autoscaling:DetachInstances" not in detach_result.resource_actions'

- name: detach 2 instance from the asg and replace with other instances
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand Down Expand Up @@ -189,6 +232,19 @@
- "{{ instance_replace_2 }}"
- "{{ instance_replace_3 }}"

- name: kill asg created in this test - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
state: absent
register: removed
check_mode: true

- assert:
that:
- removed is changed
- removed is not failed
- '"autoscaling:DeleteAutoScalingGroup" not in removed.resource_actions'

- name: kill asg created in this test
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
Expand All @@ -198,6 +254,19 @@
ignore_errors: yes
retries: 10

- name: kill asg created in this test - check_mode (idempotent)
ec2_asg:
name: "{{ resource_prefix }}-asg-detach-test"
state: absent
register: removed
check_mode: true

- assert:
that:
- removed is not changed
- removed is not failed
- '"autoscaling:DeleteAutoScalingGroup" not in removed.resource_actions'

- name: remove launch config created in this test
ec2_lc:
name: "{{ resource_prefix }}-lc-detach-test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@
that:
- info_result.results[0].tags | length == 0

- name: Tag asg - check_mode
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
tags:
- tag_a: 'value 1'
propagate_at_launch: no
- tag_b: 'value 2'
propagate_at_launch: yes
register: output
check_mode: true

- assert:
that:
- output is changed
- output is not failed
- '"autoscaling:CreateOrUpdateTags" not in output.resource_actions'

- name: Tag asg
ec2_asg:
name: "{{ resource_prefix }}-asg-tag-test"
Expand Down