From bd129bbc6c5bc1cf713be058653fb0ccab42ffbe Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Thu, 24 Mar 2022 22:03:18 -0700 Subject: [PATCH 1/4] Add check_mode support --- plugins/modules/ec2_asg.py | 10 +++ .../ec2_asg/tasks/create_update_delete.yml | 24 +++++++ .../roles/ec2_asg/tasks/instance_detach.yml | 69 +++++++++++++++++++ .../roles/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/roles/ec2_asg/tasks/create_update_delete.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/create_update_delete.yml index 75667f3e68a..fe1470c80c5 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 @@ -415,6 +415,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/roles/ec2_asg/tasks/instance_detach.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/instance_detach.yml index 3b524b08077..955a1e29913 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 + 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/roles/ec2_asg/tasks/tag_operations.yml b/tests/integration/targets/ec2_asg/roles/ec2_asg/tasks/tag_operations.yml index 2f9cc118c4f..e6770c738e5 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 + 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" From 692b4743114af7311556d29289ca9f39d76fe481 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Wed, 30 Mar 2022 08:06:43 -0700 Subject: [PATCH 2/4] Add check_mode support --- .../ec2_asg/roles/ec2_asg/tasks/create_update_delete.yml | 2 +- .../targets/ec2_asg/roles/ec2_asg/tasks/instance_detach.yml | 4 ++-- .../targets/ec2_asg/roles/ec2_asg/tasks/tag_operations.yml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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 fe1470c80c5..f37c37f90f1 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 @@ -415,7 +415,7 @@ groups: - "{{ sg.group_id }}" - - name: update autoscaling group with mixed-instances policy with mixed instances types + - name: update autoscaling group with mixed-instances policy with mixed instances types - check_mode ec2_asg: name: "{{ resource_prefix }}-asg" launch_template: 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 955a1e29913..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 @@ -19,7 +19,7 @@ #---------------------------------------------------------------------- - - name: create a AutoScalingGroup to be used for instance_detach test + - 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" @@ -89,7 +89,7 @@ #---------------------------------------------------------------------- - - name: detach 2 instance from the asg and replace with other instances + - 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" 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 e6770c738e5..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,7 +49,7 @@ that: - info_result.results[0].tags | length == 0 - - name: Tag asg + - name: Tag asg - check_mode ec2_asg: name: "{{ resource_prefix }}-asg-tag-test" tags: From cbdd8fd09a685dbdd24314bb887466281b6a8d39 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Thu, 31 Mar 2022 11:42:45 -0700 Subject: [PATCH 3/4] Add changelogs fragment --- changelogs/fragments/1033-ec2_asg-check-mode-support.yml | 2 ++ 1 file changed, 2 insertions(+) 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). From 5265c05755a9a598d48a98f1cd6b86197a88c870 Mon Sep 17 00:00:00 2001 From: mandar242 Date: Fri, 1 Apr 2022 11:04:08 -0700 Subject: [PATCH 4/4] Add more integration tests --- plugins/modules/ec2_asg.py | 2 +- .../ec2_asg/tasks/create_update_delete.yml | 31 +++++++++++++++++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/plugins/modules/ec2_asg.py b/plugins/modules/ec2_asg.py index f38564bba97..f95fb329ce5 100644 --- a/plugins/modules/ec2_asg.py +++ b/plugins/modules/ec2_asg.py @@ -1210,7 +1210,7 @@ def create_autoscaling_group(connection): 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.") + 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) 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 f37c37f90f1..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