From 3cc93db2d2333ea406b11d776a1a0d7d231d8e44 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Fri, 1 Oct 2021 17:16:41 +0200 Subject: [PATCH 1/3] ecs_taskdefinition - add placement_constraints option Signed-off-by: Alina Buzachis --- ecs_taskdefinition.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/ecs_taskdefinition.py b/ecs_taskdefinition.py index 86319b8bb3c..de02167a050 100644 --- a/ecs_taskdefinition.py +++ b/ecs_taskdefinition.py @@ -518,6 +518,22 @@ - If I(launch_type=FARGATE), this field is required and is limited by the CPU. required: false type: str + placement_constraints: + version_added: 2.1.0 + description: + - Placement constraint objects to use for the task. + - You can specify a maximum of 10 constraints per task. + - Task placement constraints are not supported for tasks run on Fargate. + required: false + type: list + elements: dict + suboptions: + type: + description: The type of constraint. + type: str + expression: + description: A cluster query language expression to apply to the constraint. + type: str extends_documentation_fragment: - amazon.aws.aws - amazon.aws.ec2 @@ -667,7 +683,7 @@ def describe_task(self, task_name): except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: return None - def register_task(self, family, task_role_arn, execution_role_arn, network_mode, container_definitions, volumes, launch_type, cpu, memory): + def register_task(self, family, task_role_arn, execution_role_arn, network_mode, container_definitions, volumes, launch_type, cpu, memory, placement_constraints): validated_containers = [] # Ensures the number parameters are int as required by boto @@ -721,6 +737,8 @@ def register_task(self, family, task_role_arn, execution_role_arn, network_mode, params['requiresCompatibilities'] = [launch_type] if execution_role_arn: params['executionRoleArn'] = execution_role_arn + if placement_constraints: + params['placementConstraints'] = placement_constraints try: response = self.ecs.register_task_definition(aws_retry=True, **params) @@ -780,7 +798,8 @@ def main(): volumes=dict(required=False, type='list', elements='dict'), launch_type=dict(required=False, choices=['EC2', 'FARGATE']), cpu=dict(), - memory=dict(required=False, type='str') + memory=dict(required=False, type='str'), + placement_constraints=dict(required=False, type='list', elements='dict') ) module = AnsibleAWSModule(argument_spec=argument_spec, @@ -801,8 +820,12 @@ def main(): network_mode = module.params['network_mode'] launch_type = module.params['launch_type'] - if launch_type == 'FARGATE' and network_mode != 'awsvpc': - module.fail_json(msg="To use FARGATE launch type, network_mode must be awsvpc") + placement_constraints = module.params['placement_constraints'] + if launch_type == 'FARGATE': + if network_mode != 'awsvpc': + module.fail_json(msg="To use FARGATE launch type, network_mode must be awsvpc") + if placement_constraints: + module.fail_json(msg="Task placement constraints are not supported for tasks run on Fargate") for container in module.params['containers']: if container.get('links') and network_mode == 'awsvpc': @@ -969,7 +992,8 @@ def _task_definition_matches(requested_volumes, requested_containers, requested_ volumes, module.params['launch_type'], module.params['cpu'], - module.params['memory']) + module.params['memory'], + module.params['placement_constraints'],) results['changed'] = True elif module.params['state'] == 'absent': From 649850ca613f37cf96c0c99c091828e807791389 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Fri, 8 Oct 2021 14:19:06 +0200 Subject: [PATCH 2/3] Address reviewer's comments Signed-off-by: Alina Buzachis --- ecs_taskdefinition.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecs_taskdefinition.py b/ecs_taskdefinition.py index de02167a050..41a8c9e610f 100644 --- a/ecs_taskdefinition.py +++ b/ecs_taskdefinition.py @@ -799,7 +799,7 @@ def main(): launch_type=dict(required=False, choices=['EC2', 'FARGATE']), cpu=dict(), memory=dict(required=False, type='str'), - placement_constraints=dict(required=False, type='list', elements='dict') + placement_constraints=dict(required=False, type='list', elements='dict', options=dict(type=dict(type='str'), expression=dict(type='str')))), ) module = AnsibleAWSModule(argument_spec=argument_spec, From 7a1c2fbc0d689ae0418ea6eeb6d30a7d479f95cd Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Sat, 2 Oct 2021 17:52:09 +0200 Subject: [PATCH 3/3] pep8 fixup - line too long --- ecs_taskdefinition.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ecs_taskdefinition.py b/ecs_taskdefinition.py index 41a8c9e610f..144426670a9 100644 --- a/ecs_taskdefinition.py +++ b/ecs_taskdefinition.py @@ -683,7 +683,8 @@ def describe_task(self, task_name): except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: return None - def register_task(self, family, task_role_arn, execution_role_arn, network_mode, container_definitions, volumes, launch_type, cpu, memory, placement_constraints): + def register_task(self, family, task_role_arn, execution_role_arn, network_mode, container_definitions, + volumes, launch_type, cpu, memory, placement_constraints): validated_containers = [] # Ensures the number parameters are int as required by boto @@ -799,7 +800,8 @@ def main(): launch_type=dict(required=False, choices=['EC2', 'FARGATE']), cpu=dict(), memory=dict(required=False, type='str'), - placement_constraints=dict(required=False, type='list', elements='dict', options=dict(type=dict(type='str'), expression=dict(type='str')))), + placement_constraints=dict(required=False, type='list', elements='dict', + options=dict(type=dict(type='str'), expression=dict(type='str'))), ) module = AnsibleAWSModule(argument_spec=argument_spec,