From 818d057b678347375ad25cbb759fd2d09b0977b5 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 24 Mar 2022 22:03:18 -0700 Subject: [PATCH] Add check_mode support --- plugins/modules/ec2_asg.py | 10 +++ .../targets/ec2_asg/tasks/instance_detach.yml | 69 +++++++++++++++++++ .../targets/ec2_asg/tasks/main.yml | 24 +++++++ .../targets/ec2_asg/tasks/tag_operations.yml | 17 +++++ 4 files changed, 120 insertions(+) diff --git a/plugins/modules/ec2_asg.py b/plugins/modules/ec2_asg.py index fa91232cbe6..f38564bba97 100644 --- a/plugins/modules/ec2_asg.py +++ b/plugins/modules/ec2_asg.py @@ -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']] @@ -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 not in check_mode.") + as_group = as_groups[0] initial_asg_properties = get_properties(as_group) changed = False @@ -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) @@ -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') @@ -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'], diff --git a/tests/integration/targets/ec2_asg/tasks/instance_detach.yml b/tests/integration/targets/ec2_asg/tasks/instance_detach.yml index fbd29d39640..5cecfdb27a5 100644 --- a/tests/integration/targets/ec2_asg/tasks/instance_detach.yml +++ b/tests/integration/targets/ec2_asg/tasks/instance_detach.yml @@ -18,6 +18,27 @@ - '"autoscaling:CreateLaunchConfiguration" in create_lc.resource_actions' #---------------------------------------------------------------------- + + - name: create a AutoScalingGroup to be used for instance_detach test + 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" @@ -68,6 +89,28 @@ #---------------------------------------------------------------------- + - name: detach 2 instance from the asg and replace with other instances + 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" @@ -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" @@ -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" diff --git a/tests/integration/targets/ec2_asg/tasks/main.yml b/tests/integration/targets/ec2_asg/tasks/main.yml index 4188f245379..efce187b653 100644 --- a/tests/integration/targets/ec2_asg/tasks/main.yml +++ b/tests/integration/targets/ec2_asg/tasks/main.yml @@ -493,6 +493,30 @@ groups: - "{{ sg.group_id }}" + - name: update autoscaling group with mixed-instances policy with mixed instances types + 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" diff --git a/tests/integration/targets/ec2_asg/tasks/tag_operations.yml b/tests/integration/targets/ec2_asg/tasks/tag_operations.yml index 2f9cc118c4f..e6770c738e5 100644 --- a/tests/integration/targets/ec2_asg/tasks/tag_operations.yml +++ b/tests/integration/targets/ec2_asg/tasks/tag_operations.yml @@ -49,6 +49,23 @@ that: - info_result.results[0].tags | length == 0 + - name: Tag asg + 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"