From c67c890726a512b5bfadf0cfa97f1534ce470711 Mon Sep 17 00:00:00 2001 From: Michael Mayer Date: Fri, 18 Mar 2022 13:29:18 -0700 Subject: [PATCH] Support changing of launch type (#840) Support changing of launch type SUMMARY When changing the launch_type parameter for an ecs_taskdefition there was no change reported by the module. This adds a check to see if launch_type in the ecs_taskdefinition has changed. If there is a change detected the module reports back there is not a matching task definition and creates a new one. ISSUE TYPE Bugfix Pull Request COMPONENT NAME ecs_taskdefinition Reviewed-by: Alina Buzachis Reviewed-by: Michael Mayer Reviewed-by: Markus Bergholz --- ...inition-fix-task-definition-comparison.yml | 2 ++ plugins/modules/ecs_taskdefinition.py | 8 +++++-- .../targets/ecs_cluster/tasks/full_test.yml | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/840-ecs_taskdefinition-fix-task-definition-comparison.yml diff --git a/changelogs/fragments/840-ecs_taskdefinition-fix-task-definition-comparison.yml b/changelogs/fragments/840-ecs_taskdefinition-fix-task-definition-comparison.yml new file mode 100644 index 00000000000..16c51815aea --- /dev/null +++ b/changelogs/fragments/840-ecs_taskdefinition-fix-task-definition-comparison.yml @@ -0,0 +1,2 @@ +bugfixes: +- ecs_taskdefinition - include launch_type comparison when comparing task definitions (https://github.com/ansible-collections/community.aws/pull/840) diff --git a/plugins/modules/ecs_taskdefinition.py b/plugins/modules/ecs_taskdefinition.py index f99db8b9659..c74bf44ec9d 100644 --- a/plugins/modules/ecs_taskdefinition.py +++ b/plugins/modules/ecs_taskdefinition.py @@ -923,13 +923,16 @@ def _right_has_values_of_left(left, right): return True - def _task_definition_matches(requested_volumes, requested_containers, requested_task_role_arn, existing_task_definition): + def _task_definition_matches(requested_volumes, requested_containers, requested_task_role_arn, requested_launch_type, existing_task_definition): if td['status'] != "ACTIVE": return None if requested_task_role_arn != td.get('taskRoleArn', ""): return None + if requested_launch_type is not None and requested_launch_type not in td.get('compatibilities', []): + return None + existing_volumes = td.get('volumes', []) or [] if len(requested_volumes) != len(existing_volumes): @@ -972,7 +975,8 @@ def _task_definition_matches(requested_volumes, requested_containers, requested_ requested_volumes = module.params['volumes'] or [] requested_containers = module.params['containers'] or [] requested_task_role_arn = module.params['task_role_arn'] - existing = _task_definition_matches(requested_volumes, requested_containers, requested_task_role_arn, td) + requested_launch_type = module.params['launch_type'] + existing = _task_definition_matches(requested_volumes, requested_containers, requested_task_role_arn, requested_launch_type, td) if existing: break diff --git a/tests/integration/targets/ecs_cluster/tasks/full_test.yml b/tests/integration/targets/ecs_cluster/tasks/full_test.yml index cb58ecc74ed..88e29cee7f8 100644 --- a/tests/integration/targets/ecs_cluster/tasks/full_test.yml +++ b/tests/integration/targets/ecs_cluster/tasks/full_test.yml @@ -674,6 +674,29 @@ ecs_taskdefinition_info: task_definition: "{{ ecs_task_name }}-vpc:{{ ecs_fargate_task_definition.taskdefinition.revision }}" + - name: create EC2 VPC-networked task definition with CPU or Memory and execution role + ecs_taskdefinition: + containers: "{{ ecs_fargate_task_containers }}" + family: "{{ ecs_task_name }}-vpc" + network_mode: awsvpc + launch_type: EC2 + cpu: 512 + memory: 1024 + execution_role_arn: "{{ iam_execution_role.arn }}" + state: present + vars: + ecs_task_host_port: 8080 + register: ecs_ec2_task_definition + + - name: obtain ECS task definition facts + ecs_taskdefinition_info: + task_definition: "{{ ecs_task_name }}-vpc:{{ ecs_ec2_task_definition.taskdefinition.revision }}" + + - name: check that changing task definiton launch type created a new task definition revision + assert: + that: + - ecs_fargate_task_definition.taskdefinition.revision != ecs_ec2_task_definition.taskdefinition.revision + - name: create fargate ECS service without network config (expected to fail) ecs_service: state: present