From ad9d36233642c4d171003276331cd0fc87c4f51a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 28 Jun 2024 22:26:34 +0200 Subject: [PATCH] Make docker_host and cli_context mutually exclusive. (#895) --- changelogs/fragments/895-docker-cli.yml | 2 ++ plugins/doc_fragments/docker.py | 4 +++- plugins/module_utils/common_cli.py | 10 +++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/895-docker-cli.yml diff --git a/changelogs/fragments/895-docker-cli.yml b/changelogs/fragments/895-docker-cli.yml new file mode 100644 index 00000000..8e225bca --- /dev/null +++ b/changelogs/fragments/895-docker-cli.yml @@ -0,0 +1,2 @@ +bugfixes: + - "docker_compose_v2*, docker_stack*, docker_image_build modules - using ``cli_context`` no longer leads to an invalid parameter combination being passed to the corresponding Docker CLI tool, unless ``docker_host`` is also provided. Combining ``cli_context`` and ``docker_host`` is no longer allowed (https://github.com/ansible-collections/community.docker/issues/892, https://github.com/ansible-collections/community.docker/pull/895)." diff --git a/plugins/doc_fragments/docker.py b/plugins/doc_fragments/docker.py index 2c78c5fa..c7565f2b 100644 --- a/plugins/doc_fragments/docker.py +++ b/plugins/doc_fragments/docker.py @@ -318,8 +318,9 @@ options: the module will automatically replace C(tcp) in the connection URL with C(https). - If the value is not specified in the task, the value of environment variable E(DOCKER_HOST) will be used instead. If the environment variable is not set, the default value will be used. + - Mutually exclusive with O(cli_context). If neither O(docker_host) nor O(cli_context) are provided, the + value V(unix:///var/run/docker.sock) is used. type: str - default: unix:///var/run/docker.sock aliases: [ docker_url ] tls_hostname: description: @@ -381,6 +382,7 @@ options: cli_context: description: - The Docker CLI context to use. + - Mutually exclusive with O(docker_host). type: str notes: diff --git a/plugins/module_utils/common_cli.py b/plugins/module_utils/common_cli.py index 60d53987..6bd9db82 100644 --- a/plugins/module_utils/common_cli.py +++ b/plugins/module_utils/common_cli.py @@ -30,7 +30,7 @@ from ansible_collections.community.docker.plugins.module_utils.util import ( # DOCKER_COMMON_ARGS = dict( docker_cli=dict(type='path'), - docker_host=dict(type='str', default=DEFAULT_DOCKER_HOST, fallback=(env_fallback, ['DOCKER_HOST']), aliases=['docker_url']), + docker_host=dict(type='str', fallback=(env_fallback, ['DOCKER_HOST']), aliases=['docker_url']), tls_hostname=dict(type='str', fallback=(env_fallback, ['DOCKER_TLS_HOSTNAME'])), api_version=dict(type='str', default='auto', fallback=(env_fallback, ['DOCKER_API_VERSION']), aliases=['docker_api_version']), ca_path=dict(type='path', aliases=['ca_cert', 'tls_ca_cert', 'cacert_path']), @@ -62,7 +62,11 @@ class AnsibleDockerClientBase(object): self.fail('Cannot find docker CLI in path. Please provide it explicitly with the docker_cli parameter') self._cli_base = [self._cli] - self._cli_base.extend(['--host', common_args['docker_host']]) + docker_host = common_args['docker_host'] + if not docker_host and not common_args['cli_context']: + docker_host = DEFAULT_DOCKER_HOST + if docker_host: + self._cli_base.extend(['--host', docker_host]) if common_args['validate_certs']: self._cli_base.append('--tlsverify') elif common_args['tls']: @@ -275,7 +279,7 @@ class AnsibleModuleDockerClient(AnsibleDockerClientBase): merged_arg_spec.update(argument_spec) self.arg_spec = merged_arg_spec - mutually_exclusive_params = [] + mutually_exclusive_params = [('docker_host', 'cli_context')] mutually_exclusive_params += DOCKER_MUTUALLY_EXCLUSIVE if mutually_exclusive: mutually_exclusive_params += mutually_exclusive