From fb9784e4c77a9ac874c53dad1fe8e6d8bdfefc54 Mon Sep 17 00:00:00 2001 From: aliou-sidibe <152864785+aliou-sidibe@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:30:32 +0100 Subject: [PATCH] Add 'detach' option to docker_stack module to control immediate exit behavior on stack deployment/remove (#987) --- changelogs/fragments/987-docker-stack-detach.yml | 2 ++ plugins/modules/docker_stack.py | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 changelogs/fragments/987-docker-stack-detach.yml diff --git a/changelogs/fragments/987-docker-stack-detach.yml b/changelogs/fragments/987-docker-stack-detach.yml new file mode 100644 index 000000000..afeea9e7a --- /dev/null +++ b/changelogs/fragments/987-docker-stack-detach.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_stack - allow to add ``--detach=false`` option to ``docker stack deploy`` command (https://github.com/ansible-collections/community.docker/pull/987)." diff --git a/plugins/modules/docker_stack.py b/plugins/modules/docker_stack.py index 728bc5cfc..9848edd81 100644 --- a/plugins/modules/docker_stack.py +++ b/plugins/modules/docker_stack.py @@ -57,6 +57,14 @@ current stack definition. type: bool default: false + detach: + description: + - If V(false), the C(--detach=false) option is added to the C(docker stack deploy) command, + allowing Docker to wait for tasks to converge before exiting. + - If V(true) (default), Docker exits immediately instead of waiting for tasks to converge. + type: bool + default: true + version_added: 4.1.0 with_registry_auth: description: - If true will add the C(--with-registry-auth) option to the C(docker stack deploy) command. @@ -200,6 +208,8 @@ def docker_stack_deploy(client, stack_name, compose_files): command = ["stack", "deploy"] if client.module.params["prune"]: command += ["--prune"] + if not client.module.params["detach"]: + command += ["--detach=false"] if client.module.params["with_registry_auth"]: command += ["--with-registry-auth"] if client.module.params["resolve_image"]: @@ -222,6 +232,8 @@ def docker_stack_inspect(client, stack_name): def docker_stack_rm(client, stack_name, retries, interval): command = ["stack", "rm", stack_name] + if not client.module.params["detach"]: + command += ["--detach=false"] rc, out, err = client.call_cli(*command) while to_native(err) != "Nothing found in stack: %s\n" % stack_name and retries > 0: @@ -237,6 +249,7 @@ def main(): 'name': dict(type='str', required=True), 'compose': dict(type='list', elements='raw', default=[]), 'prune': dict(type='bool', default=False), + 'detach': dict(type='bool', default=True), 'with_registry_auth': dict(type='bool', default=False), 'resolve_image': dict(type='str', choices=['always', 'changed', 'never']), 'state': dict(type='str', default='present', choices=['present', 'absent']),