From 3324d0a4b0d515f461394767b973f4e74734e3ef Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 9 May 2017 15:19:27 -0500 Subject: [PATCH] Support auto_remove in docker_container (#22929) * Support auto_remove in docker_container * Fail if not docker>=2 and auto_remove=True, don't set auto_remove in host_config if not docker>=2 * Make quoting more readable in ansible errors --- .../modules/cloud/docker/docker_container.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 9b4e92300ae..4056348872c 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -35,6 +35,11 @@ description: version_added: "2.1" options: + auto_remove: + description: + - enable auto-removal of the container on daemon side when the container's process exits + default: false + version_added: "2.4" blkio_weight: description: - Block IO (relative weight), between 10 and 1000. @@ -688,6 +693,7 @@ class TaskParameters(DockerBaseClass): super(TaskParameters, self).__init__() self.client = client + self.auto_remove = None self.blkio_weight = None self.capabilities = None self.cleanup = None @@ -942,6 +948,11 @@ class TaskParameters(DockerBaseClass): devices='devices', pid_mode='pid_mode' ) + + if HAS_DOCKER_PY_2: + # auto_remove is only supported in docker>=2 + host_config_params['auto_remove'] = 'auto_remove' + params = dict() for key, value in host_config_params.items(): if getattr(self, value, None) is not None: @@ -1228,6 +1239,7 @@ class Container(DockerBaseClass): # Map parameters to container inspect results config_mapping = dict( + auto_remove=host_config.get('AutoRemove'), image=config.get('Image'), expected_cmd=config.get('Cmd'), hostname=config.get('Hostname'), @@ -1954,6 +1966,7 @@ class ContainerManager(DockerBaseClass): def main(): argument_spec = dict( + auto_remove=dict(type='bool', default=False), blkio_weight=dict(type='int'), capabilities=dict(type='list'), cleanup=dict(type='bool', default=False), @@ -2035,6 +2048,9 @@ def main(): supports_check_mode=True ) + if not HAS_DOCKER_PY_2 and client.module.params.get('auto_remove'): + client.module.fail_json(msg="'auto_remove' is not compatible with docker-py, and requires the docker python module") + cm = ContainerManager(client) client.module.exit_json(**cm.results)