From eccf4a5ddadabb21173ab76e996133d91319e176 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Wed, 16 Oct 2024 11:23:51 -0400 Subject: [PATCH] Prepare ec2_placement_group* module for promotion (#2167) SUMMARY This PR refactors ec2_placement_group*. Depends-On: ansible-collections/amazon.aws#2322 Refer: https://issues.redhat.com/browse/ACA-1886 ISSUE TYPE Bugfix Pull Request Docs Pull Request Feature Pull Request New Module Pull Request COMPONENT NAME ADDITIONAL INFORMATION Reviewed-by: Bikouo Aubin Reviewed-by: GomathiselviS Reviewed-by: Alina Buzachis --- .../refactor_ec2_placement_group.yml | 2 + plugins/modules/ec2_placement_group.py | 125 +++++++------- plugins/modules/ec2_placement_group_info.py | 45 +++-- .../ec2_placement_group/tasks/env_cleanup.yml | 34 ++-- .../ec2_placement_group/tasks/env_setup.yml | 16 +- .../ec2_placement_group/tasks/main.yml | 155 +++++++++--------- 6 files changed, 187 insertions(+), 190 deletions(-) create mode 100644 changelogs/fragments/refactor_ec2_placement_group.yml diff --git a/changelogs/fragments/refactor_ec2_placement_group.yml b/changelogs/fragments/refactor_ec2_placement_group.yml new file mode 100644 index 00000000000..c4366ed2c1f --- /dev/null +++ b/changelogs/fragments/refactor_ec2_placement_group.yml @@ -0,0 +1,2 @@ +minor_changes: + - ec2_placement_group - Refactor module to use shared code from ``amazon.aws.plugins.module_utils.ec2`` and update ``RETURN`` block (https://github.com/ansible-collections/community.aws/pull/2167). diff --git a/plugins/modules/ec2_placement_group.py b/plugins/modules/ec2_placement_group.py index b045ea34b25..1a38252ac97 100644 --- a/plugins/modules/ec2_placement_group.py +++ b/plugins/modules/ec2_placement_group.py @@ -25,8 +25,8 @@ partition_count: description: - The number of partitions. - - Valid only when I(Strategy) is set to C(partition). - - Must be a value between C(1) and C(7). + - Valid only when O(strategy) is set to V(partition). + - Must be a value between V(1) and V(7). type: int version_added: 3.1.0 state: @@ -86,23 +86,42 @@ placement_group: description: Placement group attributes returned: when state != absent - type: complex + type: dict contains: + group_arn: + description: Placement Group ARN. + type: str + returned: always + sample: "arn:aws:ec2:us-east-1:123456789012:placement-group" + group_id: + description: Placement Group Id. + type: str + returned: always + sample: "pg-123456789012" name: - description: PG name + description: Placement Group name. + type: str + returned: always + sample: "my-cluster" + partition_count: + description: Partition Count. type: str - sample: my-cluster + returned: If applicable + sample: "my-cluster" state: - description: PG state + description: Placement Groupt state. type: str + returned: If applicable sample: "available" strategy: - description: PG strategy + description: Placement Group strategy. type: str + returned: If applicable sample: "cluster" tags: - description: Tags associated with the placement group + description: Tags associated with the placement group. type: dict + returned: If applicable version_added: 8.1.0 sample: tags: @@ -110,59 +129,45 @@ other: value2 """ -try: - import botocore -except ImportError: - pass # caught by AnsibleAWSModule +from typing import Any +from typing import Dict -from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code -from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import create_ec2_placement_group +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import delete_ec2_placement_group +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_placement_groups from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_specifications from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule -@AWSRetry.exponential_backoff() -def search_placement_group(connection, module): +def search_placement_group(connection, name: str) -> Dict[str, Any]: """ Check if a placement group exists. """ - name = module.params.get("name") - try: - response = connection.describe_placement_groups(Filters=[{"Name": "group-name", "Values": [name]}]) - except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg=f"Couldn't find placement group named [{name}]") + response = describe_ec2_placement_groups(connection, Filters=[{"Name": "group-name", "Values": [name]}]) - if len(response["PlacementGroups"]) != 1: + if len(response) != 1: return None else: - placement_group = response["PlacementGroups"][0] - return { - "name": placement_group["GroupName"], - "state": placement_group["State"], - "strategy": placement_group["Strategy"], - "tags": boto3_tag_list_to_ansible_dict(placement_group.get("Tags")), - } + return format_placement_group_information(response[0]) -@AWSRetry.exponential_backoff(catch_extra_error_codes=["InvalidPlacementGroup.Unknown"]) -def get_placement_group_information(connection, name): +def format_placement_group_information(response: Dict[str, Any]) -> Dict[str, Any]: """ - Retrieve information about a placement group. + Format placement group information """ - response = connection.describe_placement_groups(GroupNames=[name]) - placement_group = response["PlacementGroups"][0] - return { - "name": placement_group["GroupName"], - "state": placement_group["State"], - "strategy": placement_group["Strategy"], - "tags": boto3_tag_list_to_ansible_dict(placement_group.get("Tags")), - } - - -@AWSRetry.exponential_backoff() -def create_placement_group(connection, module): + + response = camel_dict_to_snake_dict(response, ignore_list=["Tags"]) + if "tags" in response: + response["tags"] = boto3_tag_list_to_ansible_dict(response.get("tags", [])) + response["name"] = response["group_name"] + return response + + +def create_placement_group(connection, module: AnsibleAWSModule) -> None: name = module.params.get("name") strategy = module.params.get("strategy") tags = module.params.get("tags") @@ -178,38 +183,26 @@ def create_placement_group(connection, module): params["TagSpecifications"] = boto3_tag_specifications(tags, types=["placement-group"]) if partition_count: params["PartitionCount"] = partition_count - params["DryRun"] = module.check_mode - - try: - connection.create_placement_group(**params) - except is_boto3_error_code("DryRunOperation"): + if module.check_mode: module.exit_json( changed=True, placement_group={ "name": name, - "state": "DryRun", "strategy": strategy, "tags": tags, }, + msg="EC2 placement group would be created if not in check mode", ) - except ( - botocore.exceptions.ClientError, - botocore.exceptions.BotoCoreError, - ) as e: # pylint: disable=duplicate-except - module.fail_json_aws(e, msg=f"Couldn't create placement group [{name}]") - module.exit_json(changed=True, placement_group=get_placement_group_information(connection, name)) + response = create_ec2_placement_group(connection, **params) + module.exit_json(changed=True, placement_group=format_placement_group_information(response)) -@AWSRetry.exponential_backoff() -def delete_placement_group(connection, module): +def delete_placement_group(connection, module: AnsibleAWSModule) -> None: + if module.check_mode: + module.exit_json(changed=True, msg="VPC would be deleted if not in check mode") name = module.params.get("name") - - try: - connection.delete_placement_group(GroupName=name, DryRun=module.check_mode) - except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: - module.fail_json_aws(e, msg=f"Couldn't delete placement group [{name}]") - + delete_ec2_placement_group(connection, name) module.exit_json(changed=True) @@ -227,9 +220,10 @@ def main(): connection = module.client("ec2") state = module.params.get("state") + name = module.params.get("name") + placement_group = search_placement_group(connection, name) if state == "present": - placement_group = search_placement_group(connection, module) if placement_group is None: create_placement_group(connection, module) else: @@ -243,7 +237,6 @@ def main(): ) elif state == "absent": - placement_group = search_placement_group(connection, module) if placement_group is None: module.exit_json(changed=False) else: diff --git a/plugins/modules/ec2_placement_group_info.py b/plugins/modules/ec2_placement_group_info.py index 74b32558246..8c67e2b5fa4 100644 --- a/plugins/modules/ec2_placement_group_info.py +++ b/plugins/modules/ec2_placement_group_info.py @@ -58,7 +58,7 @@ name: description: PG name type: str - sample: my-cluster + sample: "my-cluster" state: description: PG state type: str @@ -77,36 +77,28 @@ other: value2 """ -try: - from botocore.exceptions import BotoCoreError - from botocore.exceptions import ClientError -except ImportError: - pass # caught by AnsibleAWSModule +from typing import Any +from typing import Dict +from typing import List +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_ec2_placement_groups +from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict -from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule - -def get_placement_groups_details(connection, module): - names = module.params.get("names") - try: - if len(names) > 0: - response = connection.describe_placement_groups( - Filters=[ - { - "Name": "group-name", - "Values": names, - } - ] - ) - else: - response = connection.describe_placement_groups() - except (BotoCoreError, ClientError) as e: - module.fail_json_aws(e, msg=f"Couldn't find placement groups named [{names}]") +def get_placement_groups_details(connection, names: List) -> Dict[str, Any]: + params = {} + if len(names) > 0: + params["Filters"] = [ + { + "Name": "group-name", + "Values": names, + } + ] + response = describe_ec2_placement_groups(connection, **params) results = [] - for placement_group in response["PlacementGroups"]: + for placement_group in response: results.append( { "name": placement_group["GroupName"], @@ -129,8 +121,9 @@ def main(): ) connection = module.client("ec2") + names = module.params.get("names") - placement_groups = get_placement_groups_details(connection, module) + placement_groups = get_placement_groups_details(connection, names) module.exit_json(changed=False, placement_groups=placement_groups) diff --git a/tests/integration/targets/ec2_placement_group/tasks/env_cleanup.yml b/tests/integration/targets/ec2_placement_group/tasks/env_cleanup.yml index ce626b69c3d..be84fe8f17a 100644 --- a/tests/integration/targets/ec2_placement_group/tasks/env_cleanup.yml +++ b/tests/integration/targets/ec2_placement_group/tasks/env_cleanup.yml @@ -1,5 +1,5 @@ -- name: remove any instances in the test VPC - ec2_instance: +- name: Remove any instances in the test VPC + amazon.aws.ec2_instance: filters: vpc_id: "{{ testing_vpc.vpc.id }}" state: absent @@ -9,13 +9,13 @@ retries: 10 - name: Get ENIs - ec2_eni_info: + amazon.aws.ec2_eni_info: filters: vpc-id: "{{ testing_vpc.vpc.id }}" register: enis -- name: delete all ENIs - ec2_eni: +- name: Delete all ENIs + amazon.aws.ec2_eni: eni_id: "{{ item.id }}" state: absent until: removed is not failed @@ -23,8 +23,8 @@ ignore_errors: yes retries: 10 -- name: remove the security group - ec2_security_group: +- name: Remove the security group + amazon.aws.ec2_security_group: name: "{{ resource_prefix }}-sg" description: a security group for ansible tests vpc_id: "{{ testing_vpc.vpc.id }}" @@ -34,8 +34,8 @@ ignore_errors: yes retries: 10 -- name: remove routing rules - ec2_vpc_route_table: +- name: Remove routing rules + amazon.aws.ec2_vpc_route_table: state: absent vpc_id: "{{ testing_vpc.vpc.id }}" tags: @@ -51,8 +51,8 @@ ignore_errors: yes retries: 10 -- name: remove internet gateway - ec2_vpc_igw: +- name: Remove internet gateway + amazon.aws.ec2_vpc_igw: vpc_id: "{{ testing_vpc.vpc.id }}" state: absent register: removed @@ -60,8 +60,8 @@ ignore_errors: yes retries: 10 -- name: remove subnet A - ec2_vpc_subnet: +- name: Remove subnet A + amazon.aws.ec2_vpc_subnet: state: absent vpc_id: "{{ testing_vpc.vpc.id }}" cidr: 10.22.32.0/24 @@ -70,8 +70,8 @@ ignore_errors: yes retries: 10 -- name: remove subnet B - ec2_vpc_subnet: +- name: Remove subnet B + amazon.aws.ec2_vpc_subnet: state: absent vpc_id: "{{ testing_vpc.vpc.id }}" cidr: 10.22.33.0/24 @@ -80,8 +80,8 @@ ignore_errors: yes retries: 10 -- name: remove the VPC - ec2_vpc_net: +- name: Remove the VPC + amazon.aws.ec2_vpc_net: name: "{{ resource_prefix }}-vpc" cidr_block: 10.22.32.0/23 state: absent diff --git a/tests/integration/targets/ec2_placement_group/tasks/env_setup.yml b/tests/integration/targets/ec2_placement_group/tasks/env_setup.yml index d48bae66c83..54fa62a35db 100644 --- a/tests/integration/targets/ec2_placement_group/tasks/env_setup.yml +++ b/tests/integration/targets/ec2_placement_group/tasks/env_setup.yml @@ -1,5 +1,5 @@ - name: Create VPC for use in testing - ec2_vpc_net: + amazon.aws.ec2_vpc_net: name: "{{ resource_prefix }}-vpc" cidr_block: 10.22.32.0/23 tags: @@ -8,7 +8,7 @@ register: testing_vpc - name: Create internet gateway for use in testing - ec2_vpc_igw: + amazon.aws.ec2_vpc_igw: vpc_id: "{{ testing_vpc.vpc.id }}" state: present tags: @@ -16,7 +16,7 @@ register: igw - name: Create default subnet in zone A - ec2_vpc_subnet: + amazon.aws.ec2_vpc_subnet: state: present vpc_id: "{{ testing_vpc.vpc.id }}" cidr: 10.22.32.0/24 @@ -26,7 +26,7 @@ register: testing_subnet_a - name: Create secondary subnet in zone B - ec2_vpc_subnet: + amazon.aws.ec2_vpc_subnet: state: present vpc_id: "{{ testing_vpc.vpc.id }}" cidr: 10.22.33.0/24 @@ -35,8 +35,8 @@ Name: "{{ resource_prefix }}-subnet-b" register: testing_subnet_b -- name: create routing rules - ec2_vpc_route_table: +- name: Create routing rules + amazon.aws.ec2_vpc_route_table: vpc_id: "{{ testing_vpc.vpc.id }}" tags: created: "{{ resource_prefix }}-route" @@ -47,8 +47,8 @@ - "{{ testing_subnet_a.subnet.id }}" - "{{ testing_subnet_b.subnet.id }}" -- name: create a security group with the vpc - ec2_security_group: +- name: Create a security group with the vpc + amazon.aws.ec2_security_group: name: "{{ resource_prefix }}-sg" description: a security group for ansible tests vpc_id: "{{ testing_vpc.vpc.id }}" diff --git a/tests/integration/targets/ec2_placement_group/tasks/main.yml b/tests/integration/targets/ec2_placement_group/tasks/main.yml index eec1b168ef0..130f347db97 100644 --- a/tests/integration/targets/ec2_placement_group/tasks/main.yml +++ b/tests/integration/targets/ec2_placement_group/tasks/main.yml @@ -12,7 +12,7 @@ block: - - name: set up environment for testing. + - name: Set up environment for testing. include_tasks: env_setup.yml - name: Create a placement group 1 - check_mode @@ -22,12 +22,11 @@ check_mode: true register: pg_1_create_check_mode - - assert: + - name: Assert that placement group data is returned (check mode) + ansible.builtin.assert: that: - pg_1_create_check_mode is changed - pg_1_create_check_mode.placement_group.name == resource_prefix ~ '-pg1' - - pg_1_create_check_mode.placement_group.state == "DryRun" - - '"ec2:CreatePlacementGroup" in pg_1_create_check_mode.resource_actions' - name: Create a placement group 1 community.aws.ec2_placement_group: @@ -35,10 +34,11 @@ state: present register: pg_1_create - - set_fact: + - ansible.builtin.set_fact: placement_group_names: "{{ placement_group_names + [pg_1_create.placement_group.name] }}" - - assert: + - name: Assert that placement group is created + ansible.builtin.assert: that: - pg_1_create is changed - pg_1_create.placement_group.name == resource_prefix ~ '-pg1' @@ -51,7 +51,8 @@ - '{{ resource_prefix }}-pg1' register: pg_1_info_result - - assert: + - name: Assert that placement group is created + ansible.builtin.assert: that: - pg_1_info_result is not changed - pg_1_info_result.placement_groups[0].name == resource_prefix ~ '-pg1' @@ -65,7 +66,8 @@ state: present register: pg_1_create - - assert: + - name: Assert that placement group is not created (idempotent) + ansible.builtin.assert: that: - pg_1_create is not changed - pg_1_create.placement_group.name == resource_prefix ~ '-pg1' @@ -79,12 +81,12 @@ check_mode: true register: pg_1_create_check_mode_idem - - assert: + - name: Assert that placement group is not created (idempotent - check_mode) + ansible.builtin.assert: that: - pg_1_create_check_mode_idem is not changed - pg_1_create_check_mode_idem.placement_group.name == resource_prefix ~ '-pg1' - pg_1_create_check_mode_idem.placement_group.state == "available" - - '"ec2:CreatePlacementGroup" not in pg_1_create_check_mode_idem.resource_actions' - name: Create a placement group 2 - check_mode community.aws.ec2_placement_group: @@ -94,12 +96,11 @@ check_mode: true register: pg_2_create_check_mode - - assert: + - name: Assert that placement group is created + ansible.builtin.assert: that: - pg_2_create_check_mode is changed - pg_2_create_check_mode.placement_group.name == resource_prefix ~ '-pg2' - - pg_2_create_check_mode.placement_group.state == "DryRun" - - '"ec2:CreatePlacementGroup" in pg_2_create_check_mode.resource_actions' - name: Create a placement group 2 with spread strategy community.aws.ec2_placement_group: @@ -108,14 +109,15 @@ strategy: spread register: pg_2_create - - assert: + - name: Assert that placement group is created + ansible.builtin.assert: that: - pg_2_create is changed - pg_2_create.placement_group.name == resource_prefix ~ '-pg2' - pg_2_create.placement_group.state == "available" - '"ec2:CreatePlacementGroup" in pg_2_create.resource_actions' - - set_fact: + - ansible.builtin.set_fact: placement_group_names: "{{ placement_group_names + [pg_2_create.placement_group.name] }}" - name: Gather information about placement group 2 @@ -124,7 +126,8 @@ - '{{ resource_prefix }}-pg2' register: pg_2_info_result - - assert: + - name: Assert that placement group is created + ansible.builtin.assert: that: - pg_2_info_result is not changed - pg_2_info_result.placement_groups[0].name == resource_prefix ~ '-pg2' @@ -139,7 +142,8 @@ strategy: spread register: pg_2_create - - assert: + - name: Assert that placement group exists (idempotent) + ansible.builtin.assert: that: - pg_2_create is not changed - pg_2_create.placement_group.name == resource_prefix ~ '-pg2' @@ -154,12 +158,12 @@ check_mode: true register: pg_2_create_check_mode_idem - - assert: + - name: Assert that placement group exists (idempotent - check_mode) + ansible.builtin.assert: that: - pg_2_create_check_mode_idem is not changed - pg_2_create_check_mode_idem.placement_group.name == resource_prefix ~ '-pg2' - pg_2_create_check_mode_idem.placement_group.state == "available" - - '"ec2:CreatePlacementGroup" not in pg_2_create_check_mode_idem.resource_actions' - name: Create a placement group 3 - check_mode community.aws.ec2_placement_group: @@ -170,12 +174,11 @@ check_mode: true register: pg_3_create_check_mode - - assert: + - name: Assert that placement group exists + ansible.builtin.assert: that: - pg_3_create_check_mode is changed - pg_3_create_check_mode.placement_group.name == resource_prefix ~ '-pg3' - - pg_3_create_check_mode.placement_group.state == "DryRun" - - '"ec2:CreatePlacementGroup" in pg_3_create_check_mode.resource_actions' - name: Create a placement group 3 with Partition strategy community.aws.ec2_placement_group: @@ -185,14 +188,15 @@ partition_count: 4 register: pg_3_create - - assert: + - name: Assert that placement group exists + ansible.builtin.assert: that: - pg_3_create is changed - pg_3_create.placement_group.name == resource_prefix ~ '-pg3' - pg_3_create.placement_group.state == "available" - '"ec2:CreatePlacementGroup" in pg_3_create.resource_actions' - - set_fact: + - ansible.builtin.set_fact: placement_group_names: "{{ placement_group_names + [pg_3_create.placement_group.name] }}" @@ -202,7 +206,8 @@ - '{{ resource_prefix }}-pg3' register: pg_3_info_result - - assert: + - name: Assert that placement group exists + ansible.builtin.assert: that: - pg_3_info_result is not changed - pg_3_info_result.placement_groups[0].name == resource_prefix ~ '-pg3' @@ -218,7 +223,8 @@ partition_count: 4 register: pg_3_create - - assert: + - name: Assert that placement group exists (idempotent) + ansible.builtin.assert: that: - pg_3_create is not changed - pg_3_create.placement_group.name == resource_prefix ~ '-pg3' @@ -234,12 +240,12 @@ check_mode: true register: pg_3_create_check_mode_idem - - assert: + - name: Assert that placement group exists (idempotent - check_mode) + ansible.builtin.assert: that: - pg_3_create_check_mode_idem is not changed - pg_3_create_check_mode_idem.placement_group.name == resource_prefix ~ '-pg3' - pg_3_create_check_mode_idem.placement_group.state == "available" - - '"ec2:CreatePlacementGroup" not in pg_3_create_check_mode_idem.resource_actions' - name: Create a placement group 4 with tags - check_mode community.aws.ec2_placement_group: @@ -252,14 +258,13 @@ check_mode: true register: pg_4_create_check_mode - - assert: + - name: Assert that placement group exists (check-mode) + ansible.builtin.assert: that: - pg_4_create_check_mode is changed - pg_4_create_check_mode.placement_group.name == resource_prefix ~ '-pg4' - - pg_4_create_check_mode.placement_group.state == "DryRun" - pg_4_create_check_mode.placement_group.tags.foo == "test1" - pg_4_create_check_mode.placement_group.tags.bar == "test2" - - '"ec2:CreatePlacementGroup" in pg_4_create_check_mode.resource_actions' - name: Create a placement group 4 with tags community.aws.ec2_placement_group: @@ -271,7 +276,8 @@ bar: test2 register: pg_4_create - - assert: + - name: Assert that placement group exists + ansible.builtin.assert: that: - pg_4_create is changed - pg_4_create.placement_group.name == resource_prefix ~ '-pg4' @@ -280,7 +286,7 @@ - pg_4_create.placement_group.tags.bar == "test2" - '"ec2:CreatePlacementGroup" in pg_4_create.resource_actions' - - set_fact: + - ansible.builtin.set_fact: placement_group_names: "{{ placement_group_names + [pg_4_create.placement_group.name] }}" - name: Gather information about placement group 4 @@ -289,7 +295,8 @@ - '{{ resource_prefix }}-pg4' register: pg_4_info_result - - assert: + - name: Assert that placement group exists + ansible.builtin.assert: that: - pg_4_info_result is not changed - pg_4_info_result.placement_groups[0].name == resource_prefix ~ '-pg4' @@ -309,7 +316,8 @@ bar: test2 register: pg_4_create - - assert: + - name: Assert that placement group exists (idempotent) + ansible.builtin.assert: that: - pg_4_create is not changed - pg_4_create.placement_group.name == resource_prefix ~ '-pg4' @@ -330,7 +338,8 @@ check_mode: true register: pg_4_create_check_mode_idem - - assert: + - name: Assert that placement group exists (idempotent - check-mode) + ansible.builtin.assert: that: - pg_4_create_check_mode_idem is not changed - pg_4_create_check_mode_idem.placement_group.name == resource_prefix ~ '-pg4' @@ -338,7 +347,6 @@ - pg_4_create_check_mode_idem.placement_group.strategy == "cluster" - pg_4_create_check_mode_idem.placement_group.tags.foo == "test1" - pg_4_create_check_mode_idem.placement_group.tags.bar == "test2" - - '"ec2:CreatePlacementGroup" not in pg_4_create_check_mode_idem.resource_actions' - name: List all placement groups. community.aws.ec2_placement_group_info: @@ -346,9 +354,6 @@ # Delete Placement Group ========================================== - # On using check_mode for delete placement group operation - # If operation would have succeeded, the error response is DryRunOperation. - # Otherwise, it is UnauthorizedOperation . - name: Delete a placement group 1 - check_mode community.aws.ec2_placement_group: name: '{{ resource_prefix }}-pg1' @@ -357,11 +362,10 @@ register: pg_1_delete_check_mode ignore_errors: true - - assert: + - name: Assert check mode (check mode) + ansible.builtin.assert: that: - - pg_1_delete_check_mode is not changed - - pg_1_delete_check_mode.error.code == 'DryRunOperation' - - '"ec2:DeletePlacementGroup" in pg_1_delete_check_mode.resource_actions' + - pg_1_delete_check_mode is changed - name: Delete a placement group 1 community.aws.ec2_placement_group: @@ -369,7 +373,8 @@ state: absent register: pg_1_delete - - assert: + - name: Assert that deletion is successful + ansible.builtin.assert: that: - pg_1_delete is changed - '"ec2:DeletePlacementGroup" in pg_1_delete.resource_actions' @@ -380,7 +385,8 @@ state: absent register: pg_1_delete - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_1_delete is not changed - '"ec2:DeletePlacementGroup" not in pg_1_delete.resource_actions' @@ -393,10 +399,10 @@ register: pg_1_delete_check_mode_idem ignore_errors: true - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_1_delete_check_mode_idem is not changed - - '"ec2:DeletePlacementGroup" not in pg_1_delete_check_mode_idem.resource_actions' - name: Delete a placement group 2 - check_mode community.aws.ec2_placement_group: @@ -406,11 +412,10 @@ register: pg_2_delete_check_mode ignore_errors: true - - assert: + - name: Assert that check mode is successful + ansible.builtin.assert: that: - - pg_2_delete_check_mode is not changed - - pg_2_delete_check_mode.error.code == 'DryRunOperation' - - '"ec2:DeletePlacementGroup" in pg_2_delete_check_mode.resource_actions' + - pg_2_delete_check_mode is changed - name: Delete a placement group 2 community.aws.ec2_placement_group: @@ -418,7 +423,8 @@ state: absent register: pg_2_delete - - assert: + - name: Assert that there is change + ansible.builtin.assert: that: - pg_2_delete is changed - '"ec2:DeletePlacementGroup" in pg_2_delete.resource_actions' @@ -429,7 +435,8 @@ state: absent register: pg_2_delete - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_2_delete is not changed - '"ec2:DeletePlacementGroup" not in pg_2_delete.resource_actions' @@ -442,10 +449,10 @@ register: pg_2_delete_check_mode_idem ignore_errors: true - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_2_delete_check_mode_idem is not changed - - '"ec2:DeletePlacementGroup" not in pg_2_delete_check_mode_idem.resource_actions' - name: Delete a placement group 3 - check_mode community.aws.ec2_placement_group: @@ -455,11 +462,10 @@ register: pg_3_delete_check_mode ignore_errors: true - - assert: + - name: Assert that there is change - check mode + ansible.builtin.assert: that: - - pg_3_delete_check_mode is not changed - - pg_3_delete_check_mode.error.code == 'DryRunOperation' - - '"ec2:DeletePlacementGroup" in pg_3_delete_check_mode.resource_actions' + - pg_3_delete_check_mode is changed - name: Delete a placement group 3 community.aws.ec2_placement_group: @@ -467,7 +473,8 @@ state: absent register: pg_3_delete - - assert: + - name: Assert that there is change + ansible.builtin.assert: that: - pg_3_delete is changed - '"ec2:DeletePlacementGroup" in pg_3_delete.resource_actions' @@ -477,8 +484,9 @@ name: '{{ resource_prefix }}-pg3' state: absent register: pg_3_delete - - - assert: + + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_3_delete is not changed - '"ec2:DeletePlacementGroup" not in pg_3_delete.resource_actions' @@ -491,10 +499,10 @@ register: pg_3_delete_check_mode_idem ignore_errors: true - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_3_delete_check_mode_idem is not changed - - '"ec2:DeletePlacementGroup" not in pg_3_delete_check_mode_idem.resource_actions' - name: Delete a placement group 4 - check_mode community.aws.ec2_placement_group: @@ -504,11 +512,10 @@ register: pg_4_delete_check_mode ignore_errors: true - - assert: + - name: Assert that there is change check mode + ansible.builtin.assert: that: - - pg_4_delete_check_mode is not changed - - pg_4_delete_check_mode.error.code == 'DryRunOperation' - - '"ec2:DeletePlacementGroup" in pg_4_delete_check_mode.resource_actions' + - pg_4_delete_check_mode is changed - name: Delete a placement group 4 @@ -517,7 +524,8 @@ state: absent register: pg_4_delete - - assert: + - name: Assert that there is change + ansible.builtin.assert: that: - pg_4_delete is changed - '"ec2:DeletePlacementGroup" in pg_4_delete.resource_actions' @@ -528,7 +536,8 @@ state: absent register: pg_4_delete - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_4_delete is not changed - '"ec2:DeletePlacementGroup" not in pg_4_delete.resource_actions' @@ -541,10 +550,10 @@ register: pg_4_delete_check_mode_idem ignore_errors: true - - assert: + - name: Assert that there is no change + ansible.builtin.assert: that: - pg_4_delete_check_mode_idem is not changed - - '"ec2:DeletePlacementGroup" not in pg_4_delete_check_mode_idem.resource_actions' always: