docker_*: make modules more robust on Docker errors (#57913)

* Improve general error behavior if a Docker error is not caught.

* Don't die when network doesn't exist.

* Add changelog.

* Make API version always available. Also catch errors when retrieving version.

* Fix error message.

* Adjust fallback error messages.
pull/55733/head
Felix Fontein 6 years ago committed by ansibot
parent 122d4164c5
commit f8f2738351

@ -0,0 +1,2 @@
bugfixes:
- "docker_* modules - improve robustness when not handled Docker errors occur."

@ -332,14 +332,14 @@ class AnsibleDockerClient(Client):
try: try:
super(AnsibleDockerClient, self).__init__(**self._connect_params) super(AnsibleDockerClient, self).__init__(**self._connect_params)
self.docker_api_version_str = self.version()['ApiVersion']
except APIError as exc: except APIError as exc:
self.fail("Docker API error: %s" % exc) self.fail("Docker API error: %s" % exc)
except Exception as exc: except Exception as exc:
self.fail("Error connecting: %s" % exc) self.fail("Error connecting: %s" % exc)
self.docker_api_version = LooseVersion(self.docker_api_version_str)
if min_docker_api_version is not None: if min_docker_api_version is not None:
self.docker_api_version_str = self.version()['ApiVersion']
self.docker_api_version = LooseVersion(self.docker_api_version_str)
if self.docker_api_version < LooseVersion(min_docker_api_version): if self.docker_api_version < LooseVersion(min_docker_api_version):
self.fail('Docker API version is %s. Minimum version required is %s.' % (self.docker_api_version_str, min_docker_api_version)) self.fail('Docker API version is %s. Minimum version required is %s.' % (self.docker_api_version_str, min_docker_api_version))

@ -443,6 +443,7 @@ import os
import re import re
import sys import sys
import tempfile import tempfile
import traceback
from contextlib import contextmanager from contextlib import contextmanager
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -452,7 +453,13 @@ try:
HAS_YAML_EXC = None HAS_YAML_EXC = None
except ImportError as exc: except ImportError as exc:
HAS_YAML = False HAS_YAML = False
HAS_YAML_EXC = str(exc) HAS_YAML_EXC = traceback.format_exc()
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
try: try:
from compose import __version__ as compose_version from compose import __version__ as compose_version
@ -463,10 +470,9 @@ try:
HAS_COMPOSE = True HAS_COMPOSE = True
HAS_COMPOSE_EXC = None HAS_COMPOSE_EXC = None
MINIMUM_COMPOSE_VERSION = '1.7.0' MINIMUM_COMPOSE_VERSION = '1.7.0'
except ImportError as exc: except ImportError as exc:
HAS_COMPOSE = False HAS_COMPOSE = False
HAS_COMPOSE_EXC = str(exc) HAS_COMPOSE_EXC = traceback.format_exc()
DEFAULT_TIMEOUT = 10 DEFAULT_TIMEOUT = 10
from ansible.module_utils.docker.common import AnsibleDockerClient, DockerBaseClass from ansible.module_utils.docker.common import AnsibleDockerClient, DockerBaseClass
@ -1090,8 +1096,11 @@ def main():
if client.module._name == 'docker_service': if client.module._name == 'docker_service':
client.module.deprecate("The 'docker_service' module has been renamed to 'docker_compose'.", version='2.12') client.module.deprecate("The 'docker_service' module has been renamed to 'docker_compose'.", version='2.12')
result = ContainerManager(client).exec_module() try:
client.module.exit_json(**result) result = ContainerManager(client).exec_module()
client.module.exit_json(**result)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -152,9 +152,10 @@ config_id:
import base64 import base64
import hashlib import hashlib
import traceback
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -281,12 +282,15 @@ def main():
min_docker_api_version='1.30', min_docker_api_version='1.30',
) )
results = dict( try:
changed=False, results = dict(
) changed=False,
)
ConfigManager(client, results)() ConfigManager(client, results)()
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -942,6 +942,7 @@ container:
import os import os
import re import re
import shlex import shlex
import traceback
from distutils.version import LooseVersion from distutils.version import LooseVersion
from ansible.module_utils.common.text.formatters import human_to_bytes from ansible.module_utils.common.text.formatters import human_to_bytes
@ -964,7 +965,7 @@ try:
from docker.types import Ulimit, LogConfig from docker.types import Ulimit, LogConfig
else: else:
from docker.utils.types import Ulimit, LogConfig from docker.utils.types import Ulimit, LogConfig
from docker.errors import APIError, NotFound from docker.errors import DockerException, APIError, NotFound
except Exception: except Exception:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -1410,11 +1411,17 @@ class TaskParameters(DockerBaseClass):
return ip return ip
for net in self.networks: for net in self.networks:
if net.get('name'): if net.get('name'):
network = self.client.inspect_network(net['name']) try:
if network.get('Driver') == 'bridge' and \ network = self.client.inspect_network(net['name'])
network.get('Options', {}).get('com.docker.network.bridge.host_binding_ipv4'): if network.get('Driver') == 'bridge' and \
ip = network['Options']['com.docker.network.bridge.host_binding_ipv4'] network.get('Options', {}).get('com.docker.network.bridge.host_binding_ipv4'):
break ip = network['Options']['com.docker.network.bridge.host_binding_ipv4']
break
except NotFound as e:
self.client.fail(
"Cannot inspect the network '{0}' to determine the default IP.".format(net['name']),
exception=traceback.format_exc()
)
return ip return ip
def _parse_publish_ports(self): def _parse_publish_ports(self):
@ -3017,8 +3024,11 @@ def main():
version='2.12' version='2.12'
) )
cm = ContainerManager(client) try:
client.module.exit_json(**sanitize_result(cm.results)) cm = ContainerManager(client)
client.module.exit_json(**sanitize_result(cm.results))
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -107,6 +107,14 @@ container:
}' }'
''' '''
import traceback
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from ansible.module_utils.docker.common import AnsibleDockerClient from ansible.module_utils.docker.common import AnsibleDockerClient
@ -121,13 +129,16 @@ def main():
min_docker_api_version='1.20', min_docker_api_version='1.20',
) )
container = client.get_container(client.module.params['name']) try:
container = client.get_container(client.module.params['name'])
client.module.exit_json( client.module.exit_json(
changed=False, changed=False,
exists=(True if container else False), exists=(True if container else False),
container=container, container=container,
) )
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -185,11 +185,13 @@ disk_usage:
''' '''
import traceback
from ansible.module_utils.docker.common import AnsibleDockerClient, DockerBaseClass from ansible.module_utils.docker.common import AnsibleDockerClient, DockerBaseClass
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# Missing Docker SDK for Python handled in ansible.module_utils.docker.common # Missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -321,12 +323,15 @@ def main():
) )
client.fail_results['can_talk_to_docker'] = True client.fail_results['can_talk_to_docker'] = True
results = dict( try:
changed=False, results = dict(
) changed=False,
)
DockerHostManager(client, results) DockerHostManager(client, results)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -410,8 +410,10 @@ image:
type: dict type: dict
sample: {} sample: {}
''' '''
import os import os
import re import re
import traceback
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -427,6 +429,7 @@ if docker_version is not None:
else: else:
from docker.auth.auth import resolve_repository_name from docker.auth.auth import resolve_repository_name
from docker.utils.utils import parse_repository_tag from docker.utils.utils import parse_repository_tag
from docker.errors import DockerException
except ImportError: except ImportError:
# missing Docker SDK for Python handled in module_utils.docker.common # missing Docker SDK for Python handled in module_utils.docker.common
pass pass
@ -914,14 +917,17 @@ def main():
'use the "force_source", "force_absent" or "force_tag" option ' 'use the "force_source", "force_absent" or "force_tag" option '
'instead, depending on what you want to force.') 'instead, depending on what you want to force.')
results = dict( try:
changed=False, results = dict(
actions=[], changed=False,
image={} actions=[],
) image={}
)
ImageManager(client, results) ImageManager(client, results)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -154,8 +154,11 @@ images:
] ]
''' '''
import traceback
try: try:
from docker import utils from docker import utils
from docker.errors import DockerException
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -234,13 +237,16 @@ def main():
if client.module._name == 'docker_image_facts': if client.module._name == 'docker_image_facts':
client.module.deprecate("The 'docker_image_facts' module has been renamed to 'docker_image_info'", version='2.12') client.module.deprecate("The 'docker_image_facts' module has been renamed to 'docker_image_info'", version='2.12')
results = dict( try:
changed=False, results = dict(
images=[] changed=False,
) images=[]
)
ImageManager(client, results) ImageManager(client, results)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -129,6 +129,13 @@ import base64
import json import json
import os import os
import re import re
import traceback
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils._text import to_bytes, to_text
from ansible.module_utils.docker.common import AnsibleDockerClient, DEFAULT_DOCKER_REGISTRY, DockerBaseClass, EMAIL_REGEX from ansible.module_utils.docker.common import AnsibleDockerClient, DEFAULT_DOCKER_REGISTRY, DockerBaseClass, EMAIL_REGEX
@ -311,16 +318,19 @@ def main():
min_docker_api_version='1.20', min_docker_api_version='1.20',
) )
results = dict( try:
changed=False, results = dict(
actions=[], changed=False,
login_result={} actions=[],
) login_result={}
)
LoginManager(client, results) LoginManager(client, results)
if 'actions' in results: if 'actions' in results:
del results['actions'] del results['actions']
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -276,6 +276,7 @@ network:
''' '''
import re import re
import traceback
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -289,6 +290,7 @@ from ansible.module_utils.docker.common import (
try: try:
from docker import utils from docker import utils
from docker.errors import DockerException
if LooseVersion(docker_version) >= LooseVersion('2.0.0'): if LooseVersion(docker_version) >= LooseVersion('2.0.0'):
from docker.types import IPAMPool, IPAMConfig from docker.types import IPAMPool, IPAMConfig
except Exception: except Exception:
@ -681,8 +683,11 @@ def main():
option_minimal_versions=option_minimal_versions, option_minimal_versions=option_minimal_versions,
) )
cm = DockerNetworkManager(client) try:
client.module.exit_json(**cm.results) cm = DockerNetworkManager(client)
client.module.exit_json(**cm.results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -103,6 +103,14 @@ network:
}' }'
''' '''
import traceback
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from ansible.module_utils.docker.common import AnsibleDockerClient from ansible.module_utils.docker.common import AnsibleDockerClient
@ -117,13 +125,16 @@ def main():
min_docker_api_version='1.21', min_docker_api_version='1.21',
) )
network = client.get_network(client.module.params['name']) try:
network = client.get_network(client.module.params['name'])
client.module.exit_json( client.module.exit_json(
changed=False, changed=False,
exists=(True if network else False), exists=(True if network else False),
network=network, network=network,
) )
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -129,8 +129,10 @@ node:
''' '''
import traceback
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -277,12 +279,15 @@ def main():
min_docker_api_version='1.25', min_docker_api_version='1.25',
) )
results = dict( try:
changed=False, results = dict(
) changed=False,
)
SwarmNodeManager(client, results) SwarmNodeManager(client, results)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -87,10 +87,12 @@ nodes:
type: list type: list
''' '''
import traceback
from ansible.module_utils.docker.swarm import AnsibleDockerSwarmClient from ansible.module_utils.docker.swarm import AnsibleDockerSwarmClient
try: try:
from docker.errors import APIError, NotFound from docker.errors import DockerException, APIError, NotFound
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -136,12 +138,15 @@ def main():
client.fail_task_if_not_swarm_manager() client.fail_task_if_not_swarm_manager()
nodes = get_node_facts(client) try:
nodes = get_node_facts(client)
client.module.exit_json( client.module.exit_json(
changed=False, changed=False,
nodes=nodes, nodes=nodes,
) )
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -177,6 +177,14 @@ builder_cache_space_reclaimed:
sample: '0' sample: '0'
''' '''
import traceback
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from distutils.version import LooseVersion from distutils.version import LooseVersion
from ansible.module_utils.docker.common import AnsibleDockerClient from ansible.module_utils.docker.common import AnsibleDockerClient
@ -214,36 +222,39 @@ def main():
msg = "Error: Docker SDK for Python's version is %s. Minimum version required for builds option is %s. Use `pip install --upgrade docker` to upgrade." msg = "Error: Docker SDK for Python's version is %s. Minimum version required for builds option is %s. Use `pip install --upgrade docker` to upgrade."
client.fail(msg % (docker_version, cache_min_version)) client.fail(msg % (docker_version, cache_min_version))
result = dict() try:
result = dict()
if client.module.params['containers']:
filters = clean_dict_booleans_for_docker_api(client.module.params.get('containers_filters')) if client.module.params['containers']:
res = client.prune_containers(filters=filters) filters = clean_dict_booleans_for_docker_api(client.module.params.get('containers_filters'))
result['containers'] = res.get('ContainersDeleted') or [] res = client.prune_containers(filters=filters)
result['containers_space_reclaimed'] = res['SpaceReclaimed'] result['containers'] = res.get('ContainersDeleted') or []
result['containers_space_reclaimed'] = res['SpaceReclaimed']
if client.module.params['images']:
filters = clean_dict_booleans_for_docker_api(client.module.params.get('images_filters')) if client.module.params['images']:
res = client.prune_images(filters=filters) filters = clean_dict_booleans_for_docker_api(client.module.params.get('images_filters'))
result['images'] = res.get('ImagesDeleted') or [] res = client.prune_images(filters=filters)
result['images_space_reclaimed'] = res['SpaceReclaimed'] result['images'] = res.get('ImagesDeleted') or []
result['images_space_reclaimed'] = res['SpaceReclaimed']
if client.module.params['networks']:
filters = clean_dict_booleans_for_docker_api(client.module.params.get('networks_filters')) if client.module.params['networks']:
res = client.prune_networks(filters=filters) filters = clean_dict_booleans_for_docker_api(client.module.params.get('networks_filters'))
result['networks'] = res.get('NetworksDeleted') or [] res = client.prune_networks(filters=filters)
result['networks'] = res.get('NetworksDeleted') or []
if client.module.params['volumes']:
filters = clean_dict_booleans_for_docker_api(client.module.params.get('volumes_filters')) if client.module.params['volumes']:
res = client.prune_volumes(filters=filters) filters = clean_dict_booleans_for_docker_api(client.module.params.get('volumes_filters'))
result['volumes'] = res.get('VolumesDeleted') or [] res = client.prune_volumes(filters=filters)
result['volumes_space_reclaimed'] = res['SpaceReclaimed'] result['volumes'] = res.get('VolumesDeleted') or []
result['volumes_space_reclaimed'] = res['SpaceReclaimed']
if client.module.params['builder_cache']:
res = client.prune_builds() if client.module.params['builder_cache']:
result['builder_cache_space_reclaimed'] = res['SpaceReclaimed'] res = client.prune_builds()
result['builder_cache_space_reclaimed'] = res['SpaceReclaimed']
client.module.exit_json(**result)
client.module.exit_json(**result)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -152,9 +152,10 @@ secret_id:
import base64 import base64
import hashlib import hashlib
import traceback
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -281,13 +282,16 @@ def main():
min_docker_api_version='1.25', min_docker_api_version='1.25',
) )
results = dict( try:
changed=False, results = dict(
secret_id='' changed=False,
) secret_id=''
)
SecretManager(client, results)() SecretManager(client, results)()
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -267,9 +267,10 @@ actions:
''' '''
import json import json
import traceback
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -659,14 +660,17 @@ def main():
option_minimal_versions=option_minimal_versions, option_minimal_versions=option_minimal_versions,
) )
results = dict( try:
changed=False, results = dict(
result='', changed=False,
actions=[] result='',
) actions=[]
)
SwarmManager(client, results)() SwarmManager(client, results)()
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -192,8 +192,10 @@ tasks:
''' '''
import traceback
try: try:
from docker.errors import APIError, NotFound from docker.errors import DockerException, APIError, NotFound
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker_common # missing Docker SDK for Python handled in ansible.module_utils.docker_common
pass pass
@ -361,13 +363,16 @@ def main():
client.fail_results['docker_swarm_active'] = client.check_if_swarm_node() client.fail_results['docker_swarm_active'] = client.check_if_swarm_node()
client.fail_results['docker_swarm_manager'] = client.check_if_swarm_manager() client.fail_results['docker_swarm_manager'] = client.check_if_swarm_manager()
results = dict( try:
changed=False, results = dict(
) changed=False,
)
DockerSwarmManager(client, results) DockerSwarmManager(client, results)
results.update(client.fail_results) results.update(client.fail_results)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -1046,6 +1046,7 @@ EXAMPLES = '''
import shlex import shlex
import time import time
import operator import operator
import traceback
from distutils.version import LooseVersion from distutils.version import LooseVersion
@ -2740,21 +2741,24 @@ def main():
option_minimal_versions=option_minimal_versions, option_minimal_versions=option_minimal_versions,
) )
dsm = DockerServiceManager(client) try:
msg, changed, rebuilt, changes, facts = dsm.run_safe() dsm = DockerServiceManager(client)
msg, changed, rebuilt, changes, facts = dsm.run_safe()
results = dict( results = dict(
msg=msg, msg=msg,
changed=changed, changed=changed,
rebuilt=rebuilt, rebuilt=rebuilt,
changes=changes, changes=changes,
swarm_service=facts, swarm_service=facts,
) )
if client.module._diff: if client.module._diff:
before, after = dsm.diff_tracker.get_before_after() before, after = dsm.diff_tracker.get_before_after()
results['diff'] = dict(before=before, after=after) results['diff'] = dict(before=before, after=after)
client.module.exit_json(**results) client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -66,6 +66,14 @@ service:
type: dict type: dict
''' '''
import traceback
try:
from docker.errors import DockerException
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from ansible.module_utils.docker.swarm import AnsibleDockerSwarmClient from ansible.module_utils.docker.swarm import AnsibleDockerSwarmClient
@ -91,13 +99,16 @@ def main():
client.fail_task_if_not_swarm_manager() client.fail_task_if_not_swarm_manager()
service = get_service_info(client) try:
service = get_service_info(client)
client.module.exit_json( client.module.exit_json(
changed=False, changed=False,
service=service, service=service,
exists=bool(service) exists=bool(service)
) )
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -125,8 +125,10 @@ volume:
sample: {} sample: {}
''' '''
import traceback
try: try:
from docker.errors import APIError from docker.errors import DockerException, APIError
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -323,8 +325,11 @@ def main():
option_minimal_versions=option_minimal_versions, option_minimal_versions=option_minimal_versions,
) )
cm = DockerVolumeManager(client) try:
client.module.exit_json(**cm.results) cm = DockerVolumeManager(client)
client.module.exit_json(**cm.results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

@ -80,8 +80,10 @@ volume:
}' }'
''' '''
import traceback
try: try:
from docker.errors import NotFound from docker.errors import DockerException, NotFound
except ImportError: except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common # missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass pass
@ -110,13 +112,16 @@ def main():
min_docker_api_version='1.21', min_docker_api_version='1.21',
) )
volume = get_existing_volume(client, client.module.params['name']) try:
volume = get_existing_volume(client, client.module.params['name'])
client.module.exit_json(
changed=False, client.module.exit_json(
exists=(True if volume else False), changed=False,
volume=volume, exists=(True if volume else False),
) volume=volume,
)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc())
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save