Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ecs_* - add waiters #1209

Merged
merged 6 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/1209-ecs_service-add-waiters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- ecs_service - add ``wait`` parameter and waiter for deleting services (https://github.com/ansible-collections/community.aws/pull/1209).
- ecs_task - add ``wait`` parameter and waiter for running and stopping tasks (https://github.com/ansible-collections/community.aws/pull/1209).
17 changes: 17 additions & 0 deletions plugins/modules/ecs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@
required: false
choices: ["DAEMON", "REPLICA"]
type: str
wait:
description:
- Whether or not to wait for the desired state.
jatorcasso marked this conversation as resolved.
Show resolved Hide resolved
type: bool
default: false
version_added: 3.4.0
markuman marked this conversation as resolved.
Show resolved Hide resolved
tremble marked this conversation as resolved.
Show resolved Hide resolved
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
Expand Down Expand Up @@ -728,6 +734,7 @@ def main():
force_new_deployment=dict(required=False, default=False, type='bool'),
force_deletion=dict(required=False, default=False, type='bool'),
deployment_configuration=dict(required=False, default={}, type='dict'),
wait=dict(required=False, default=False, type='bool'),
placement_constraints=dict(
required=False,
default=[],
Expand Down Expand Up @@ -912,6 +919,16 @@ def main():
module.params['cluster'],
module.params['force_deletion'],
)

# Wait for service to be INACTIVE prior to exiting
if module.params['wait']:

params = {}
params['services'] = [module.params['name']]
params['cluster'] = module.params['cluster']

service_mgr.ecs.get_waiter('services_inactive').wait(**params)
jatorcasso marked this conversation as resolved.
Show resolved Hide resolved

except botocore.exceptions.ClientError as e:
module.fail_json_aws(e, msg="Couldn't delete service")
results['changed'] = True
Expand Down
36 changes: 34 additions & 2 deletions plugins/modules/ecs_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
- Tags that will be added to ecs tasks on start and run
required: false
aliases: ['resource_tags']
wait:
description:
- Whether or not to wait for the desired state.
type: bool
default: false
version_added: 3.4.0
tremble marked this conversation as resolved.
Show resolved Hide resolved
extends_documentation_fragment:
- amazon.aws.aws
- amazon.aws.ec2
Expand Down Expand Up @@ -351,7 +357,8 @@ def main():
started_by=dict(required=False, type='str'), # R S
network_configuration=dict(required=False, type='dict'),
launch_type=dict(required=False, choices=['EC2', 'FARGATE']),
tags=dict(required=False, type='dict', aliases=['resource_tags'])
tags=dict(required=False, type='dict', aliases=['resource_tags']),
wait=dict(required=False, default=False, type='bool'),
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True,
Expand Down Expand Up @@ -393,7 +400,9 @@ def main():
results['task'] = existing
else:
if not module.check_mode:
results['task'] = service_mgr.run_task(

# run_task returns a list of tasks created
tasks = service_mgr.run_task(
module.params['cluster'],
module.params['task_definition'],
module.params['overrides'],
Expand All @@ -402,6 +411,18 @@ def main():
module.params['launch_type'],
module.params['tags'],
)

# Wait for task(s) to be running prior to exiting
if module.params['wait']:

params = {}
params['tasks'] = [task['taskArn'] for task in tasks]
params['cluster'] = module.params['cluster']

service_mgr.ecs.get_waiter('tasks_running').wait(**params)
jatorcasso marked this conversation as resolved.
Show resolved Hide resolved

results['task'] = tasks

results['changed'] = True

elif module.params['operation'] == 'start':
Expand All @@ -418,6 +439,7 @@ def main():
module.params['started_by'],
module.params['tags'],
)

results['changed'] = True

elif module.params['operation'] == 'stop':
Expand All @@ -431,6 +453,16 @@ def main():
module.params['cluster'],
module.params['task']
)

# Wait for task to be stopped prior to exiting
if module.params['wait']:

params = {}
params['tasks'] = [module.params['task']]
params['cluster'] = module.params['cluster']

service_mgr.ecs.get_waiter('tasks_stopped').wait(**params)
jatorcasso marked this conversation as resolved.
Show resolved Hide resolved

results['changed'] = True

module.exit_json(**results)
Expand Down
Loading