Skip to content

Commit

Permalink
Support auto_remove in docker_container (ansible#22929)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sivel authored and Chris Houseknecht committed May 9, 2017
1 parent 0a22dbd commit 3324d0a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/ansible/modules/cloud/docker/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
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.
Expand Down Expand Up @@ -688,6 +693,7 @@ def __init__(self, client):
super(TaskParameters, self).__init__()
self.client = client

self.auto_remove = None
self.blkio_weight = None
self.capabilities = None
self.cleanup = None
Expand Down Expand Up @@ -942,6 +948,11 @@ def _host_config(self):
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:
Expand Down Expand Up @@ -1228,6 +1239,7 @@ def has_different_configuration(self, image):

# 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'),
Expand Down Expand Up @@ -1954,6 +1966,7 @@ def container_stop(self, container_id):

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),
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 3324d0a

Please sign in to comment.