Skip to content

Commit

Permalink
Merge pull request ansible-collections#741 from alinabuzachis/task_de…
Browse files Browse the repository at this point in the history
…finition/placement-option

ecs_taskdefinition - add placement_constraints option

Depends-On: ansible/ansible-zuul-jobs#1179
SUMMARY

Add placement_constraints option.

Fixes: ansible-collections#714
ISSUE TYPE


Feature Pull Request

COMPONENT NAME

ecs_taskdefinition

Reviewed-by: Mark Chappell <None>
Reviewed-by: None <None>
  • Loading branch information
ansible-zuul[bot] authored Oct 15, 2021
2 parents bd2004a + 7a1c2fb commit 0737e25
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions ecs_taskdefinition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -667,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):
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
Expand Down Expand Up @@ -721,6 +738,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)
Expand Down Expand Up @@ -780,7 +799,9 @@ 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',
options=dict(type=dict(type='str'), expression=dict(type='str'))),
)

module = AnsibleAWSModule(argument_spec=argument_spec,
Expand All @@ -801,8 +822,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':
Expand Down Expand Up @@ -969,7 +994,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':
Expand Down

0 comments on commit 0737e25

Please sign in to comment.