[2.6] docker_container: fix check mode for container creation (#44760)

* docker_container: fix check mode for container creation (#42380)

* Create copy of HostConfig object before adding it to results.

* Create general sanitize function in docker_common. Using it for all return values of docker_container.

* Make Python 2.6 compatible.

* Added changelog.
This commit is contained in:
Felix Fontein 2018-09-04 23:20:34 +02:00 committed by Matt Clay
parent f61d2b1c0c
commit ae62f4be32
3 changed files with 23 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "fixes docker_container check and debug mode (https://github.com/ansible/ansible/pull/42380)"

View file

@ -120,6 +120,24 @@ if not HAS_DOCKER_PY:
pass
def sanitize_result(data):
"""Sanitize data object for return to Ansible.
When the data object contains types such as docker.types.containers.HostConfig,
Ansible will fail when these are returned via exit_json or fail_json.
HostConfig is derived from dict, but its constructor requires additional
arguments. This function sanitizes data structures by recursively converting
everything derived from dict to dict and everything derived from list (and tuple)
to a list.
"""
if isinstance(data, dict):
return dict((k, sanitize_result(v)) for k, v in data.items())
elif isinstance(data, (list, tuple)):
return [sanitize_result(v) for v in data]
else:
return data
class DockerBaseClass(object):
def __init__(self):

View file

@ -611,7 +611,7 @@ import shlex
from distutils.version import LooseVersion
from ansible.module_utils.basic import human_to_bytes
from ansible.module_utils.docker_common import HAS_DOCKER_PY_2, HAS_DOCKER_PY_3, AnsibleDockerClient, DockerBaseClass
from ansible.module_utils.docker_common import HAS_DOCKER_PY_2, HAS_DOCKER_PY_3, AnsibleDockerClient, DockerBaseClass, sanitize_result
from ansible.module_utils.six import string_types
try:
@ -1795,7 +1795,7 @@ class ContainerManager(DockerBaseClass):
self.container_remove(container.Id)
def fail(self, msg, **kwargs):
self.client.module.fail_json(msg=msg, **kwargs)
self.client.module.fail_json(msg=msg, **sanitize_result(kwargs))
def _get_container(self, container):
'''
@ -2122,7 +2122,7 @@ def main():
)
cm = ContainerManager(client)
client.module.exit_json(**cm.results)
client.module.exit_json(**sanitize_result(cm.results))
if __name__ == '__main__':