From bed775c4ea7be62ae3ffa620b478ffb67523fe7f Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 30 Nov 2021 09:14:34 +0100 Subject: [PATCH] docker_container_exec: improve handling of chdir option (#243) * Only pass chdir on when it is provided, and prevent this option from being used for Docker SDK for Python < 3.0.0. * Also fix docker_api connection plugin. --- .../fragments/243-docker_container_exec-chdir.yml | 3 +++ plugins/connection/docker_api.py | 2 +- plugins/modules/docker_container_exec.py | 10 +++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/243-docker_container_exec-chdir.yml diff --git a/changelogs/fragments/243-docker_container_exec-chdir.yml b/changelogs/fragments/243-docker_container_exec-chdir.yml new file mode 100644 index 000000000..2fc44b339 --- /dev/null +++ b/changelogs/fragments/243-docker_container_exec-chdir.yml @@ -0,0 +1,3 @@ +bugfixes: + - "docker_container_exec - ``chdir`` is only supported since Docker SDK for Python 3.0.0. Make sure that this option can only use when 3.0.0 or later is installed, and prevent passing this parameter on when ``chdir`` is not provided to this module (https://github.com/ansible-collections/community.docker/pull/243, https://github.com/ansible-collections/community.docker/issues/242)." + - "docker_api connection plugin - avoid passing an unnecessary argument to a Docker SDK for Python call that is only supported by version 3.0.0 or later (https://github.com/ansible-collections/community.docker/pull/243)." diff --git a/plugins/connection/docker_api.py b/plugins/connection/docker_api.py index e64e4c5a4..ead345a21 100644 --- a/plugins/connection/docker_api.py +++ b/plugins/connection/docker_api.py @@ -164,7 +164,7 @@ def exec_command(self, cmd, in_data=None, sudoable=False): stderr=True, stdin=need_stdin, user=self._play_context.remote_user or '', - workdir=None, + # workdir=None, - only works for Docker SDK for Python 3.0.0 and later )) exec_id = exec_data['Id'] diff --git a/plugins/modules/docker_container_exec.py b/plugins/modules/docker_container_exec.py index 8c930178f..90c6e30ac 100644 --- a/plugins/modules/docker_container_exec.py +++ b/plugins/modules/docker_container_exec.py @@ -163,8 +163,13 @@ def main(): tty=dict(type='bool', default=False), ) + option_minimal_versions = dict( + chdir=dict(docker_py_version='3.0.0'), + ) + client = AnsibleDockerClient( argument_spec=argument_spec, + option_minimal_versions=option_minimal_versions, min_docker_api_version='1.20', mutually_exclusive=[('argv', 'command')], required_one_of=[('argv', 'command')], @@ -190,6 +195,9 @@ def main(): selectors = find_selectors(client.module) try: + kwargs = {} + if chdir is not None: + kwargs['workdir'] = chdir exec_data = client.exec_create( container, argv, @@ -197,7 +205,7 @@ def main(): stderr=True, stdin=bool(stdin), user=user or '', - workdir=chdir, + **kwargs ) exec_id = exec_data['Id']