From 187014101bfda93c72dc24a5b3a4aceb942d465a Mon Sep 17 00:00:00 2001 From: londondaintta <127526961+londondaintta@users.noreply.github.com> Date: Sat, 1 Mar 2025 14:03:23 +0000 Subject: [PATCH] add `--yes` parameter to `docker compose up` (#1045) * add yes option for compose up to assume yes and prevent hanging * fix type * add default * add changelog fragment * Apply doc suggestion Co-authored-by: Felix Fontein * set version_added to 4.5.0 * use `assume_yes` to avoid clashing with yaml `yes` keyword * add version check * default self.yes to False * update description * Fail on older version Co-authored-by: Felix Fontein * update changelog fragment * update description --------- Co-authored-by: Felix Fontein --- .../fragments/1045-docker-compose-up-yes.yaml | 2 ++ plugins/modules/docker_compose_v2.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 changelogs/fragments/1045-docker-compose-up-yes.yaml diff --git a/changelogs/fragments/1045-docker-compose-up-yes.yaml b/changelogs/fragments/1045-docker-compose-up-yes.yaml new file mode 100644 index 00000000..ee1b2a93 --- /dev/null +++ b/changelogs/fragments/1045-docker-compose-up-yes.yaml @@ -0,0 +1,2 @@ +minor_changes: + - docker_compose_v2 - add ``assume_yes`` parameter for ``docker compose up`` (https://github.com/ansible-collections/community.docker/pull/1045). diff --git a/plugins/modules/docker_compose_v2.py b/plugins/modules/docker_compose_v2.py index aaa4d540..acb1e9c3 100644 --- a/plugins/modules/docker_compose_v2.py +++ b/plugins/modules/docker_compose_v2.py @@ -160,6 +160,15 @@ options: - When O(wait=true), wait at most this amount of seconds. type: int version_added: 3.8.0 + assume_yes: + description: + - When O(assume_yes=true), pass C(--yes) to assume "yes" as answer to all prompts and run non-interactively. + - Right now a prompt is asked whenever a non-matching volume should be re-created. O(assume_yes=false) + results in the question being answered by "no", which will simply re-use the existing volume. + - This option is only available on Docker Compose 2.32.0 or newer. + type: bool + default: false + version_added: 4.5.0 author: - Felix Fontein (@felixfontein) @@ -444,6 +453,8 @@ from ansible_collections.community.docker.plugins.module_utils.compose_v2 import is_failed, ) +from ansible_collections.community.docker.plugins.module_utils.version import LooseVersion + class ServicesManager(BaseComposeManager): def __init__(self, client): @@ -465,6 +476,9 @@ class ServicesManager(BaseComposeManager): self.scale = parameters['scale'] or {} self.wait = parameters['wait'] self.wait_timeout = parameters['wait_timeout'] + self.yes = parameters['assume_yes'] + if self.compose_version >= LooseVersion('2.32.0') and self.yes: + self.fail('assume_yes=true needs Docker Compose 2.32.0 or newer, not version %s' % (self.compose_version, )) for key, value in self.scale.items(): if not isinstance(key, string_types): @@ -522,6 +536,8 @@ class ServicesManager(BaseComposeManager): args.append('--no-start') if dry_run: args.append('--dry-run') + if self.yes: + args.append('--yes') args.append('--') for service in self.services: args.append(service) @@ -656,6 +672,7 @@ def main(): wait=dict(type='bool', default=False), wait_timeout=dict(type='int'), ignore_build_events=dict(type='bool', default=True), + assume_yes=dict(type='bool', default=False), ) argspec_ex = common_compose_argspec_ex() argument_spec.update(argspec_ex.pop('argspec'))