From 13a20d48e2cc1afe2434243bc41d8b8e95faddfb Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 1 Apr 2022 14:55:47 -0700 Subject: [PATCH] Add check_mode support to ec2_asg (#1033) Add check_mode support to ec2_asg SUMMARY Added check_mode support to ec2_asg. CI failure could be resolved by #1036 ISSUE TYPE Feature Pull Request COMPONENT NAME ec2_asg Reviewed-by: Markus Bergholz Reviewed-by: Jill R --- .../1033-ec2_asg-check-mode-support.yml | 2 + plugins/modules/ec2_asg.py | 10 +++ .../ec2_asg/tasks/create_update_delete.yml | 55 ++++++++++++++- .../roles/ec2_asg/tasks/instance_detach.yml | 69 +++++++++++++++++++ .../roles/ec2_asg/tasks/tag_operations.yml | 17 +++++ 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1033-ec2_asg-check-mode-support.yml diff --git a/changelogs/fragments/1033-ec2_asg-check-mode-support.yml b/changelogs/fragments/1033-ec2_asg-check-mode-support.yml new file mode 100644 index 00000000000..8f198bd9b8e --- /dev/null +++ b/changelogs/fragments/1033-ec2_asg-check-mode-support.yml @@ -0,0 +1,2 @@ +minor_changes: +- ec2_asg - add check mode support (https://github.com/ansible-collections/community.aws/pull/1033). diff --git a/plugins/modules/ec2_asg.py b/plugins/modules/ec2_asg.py index fa91232cbe6..f95fb329ce5 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 required 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/roles/ec2_asg/tasks/create_update_delete.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/create_update_delete.yml index 75667f3e68a..34710f434b1 100644 --- a/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/create_update_delete.yml +++ b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/create_update_delete.yml @@ -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" @@ -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 @@ -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" @@ -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 @@ -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" diff --git a/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/instance_detach.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/instance_detach.yml index 3b524b08077..825819c883d 100644 --- a/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/instance_detach.yml +++ b/tests/integration/targets/ec2_asg/roles/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 - 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" @@ -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" @@ -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/roles/ec2_asg/tasks/tag_operations.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/tag_operations.yml index 2f9cc118c4f..500969a4fb7 100644 --- a/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/tag_operations.yml +++ b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/tag_operations.yml @@ -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"