From 38c86b7eefc66a20a9c035d716aace582245d04f Mon Sep 17 00:00:00 2001 From: Suzuki Shunsuke Date: Fri, 25 May 2018 02:55:16 +0900 Subject: [PATCH] Add 'init' option to docker_container module to support docker's `--init` option (#34547) * Add support for `--init` option to `docker_container` module Fixes #30761 * Validate docker API's version when docker_container's init option is True https://github.com/ansible/ansible/pull/34547#pullrequestreview-122355244 https://docs.docker.com/engine/reference/commandline/run/#options init option requires docker API 1.25+ . * Fix failure of sanity test --- lib/ansible/module_utils/docker_common.py | 4 ++++ lib/ansible/modules/cloud/docker/docker_container.py | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/docker_common.py b/lib/ansible/module_utils/docker_common.py index b97fa3f35d1..1d65de6c43e 100644 --- a/lib/ansible/module_utils/docker_common.py +++ b/lib/ansible/module_utils/docker_common.py @@ -187,6 +187,10 @@ class AnsibleDockerClient(Client): except Exception as exc: self.fail("Error connecting: %s" % exc) + docker_api_version = self.version()["ApiVersion"] + if self.module.params.get("init") and LooseVersion(docker_api_version) < LooseVersion("1.25"): + self.fail("docker API version is %s. Minimum version required is 1.25 to set init option." % (docker_api_version,)) + def log(self, msg, pretty_print=False): pass # if self.debug: diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 0a1a1d9a6e4..a81c6aaa95b 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -139,6 +139,13 @@ options: description: - Repository path and tag used to create the container. If an image is not found or pull is true, the image will be pulled from the registry. If no tag is included, 'latest' will be used. + init: + description: + - Run an init inside the container that forwards signals and reaps processes. + This option requires Docker API 1.25+. + type: bool + default: 'no' + version_added: "2.6" interactive: description: - Keep stdin open after a container is launched, even if not attached. @@ -653,6 +660,7 @@ class TaskParameters(DockerBaseClass): self.hostname = None self.ignore_image = None self.image = None + self.init = None self.interactive = None self.ipc_mode = None self.keep_volumes = None @@ -898,7 +906,8 @@ class TaskParameters(DockerBaseClass): group_add='groups', devices='devices', pid_mode='pid_mode', - tmpfs='tmpfs' + tmpfs='tmpfs', + init='init' ) if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3: @@ -1999,6 +2008,7 @@ def main(): hostname=dict(type='str'), ignore_image=dict(type='bool', default=False), image=dict(type='str'), + init=dict(type='bool', default=False), interactive=dict(type='bool', default=False), ipc_mode=dict(type='str'), keep_volumes=dict(type='bool', default=True),