From fbbab7429e39a930d455619d9aa840fc441af346 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Thu, 14 Mar 2019 10:55:16 +0100 Subject: [PATCH] docker_*: report more warnings (#53710) * More warnings. * Add changelog. * Improve docstring. --- changelogs/fragments/53710-docker-warnings.yml | 3 +++ lib/ansible/module_utils/docker/common.py | 12 ++++++++++++ lib/ansible/modules/cloud/docker/docker_network.py | 2 +- .../modules/cloud/docker/docker_swarm_service.py | 5 ++++- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/53710-docker-warnings.yml diff --git a/changelogs/fragments/53710-docker-warnings.yml b/changelogs/fragments/53710-docker-warnings.yml new file mode 100644 index 0000000000..d2adb0c740 --- /dev/null +++ b/changelogs/fragments/53710-docker-warnings.yml @@ -0,0 +1,3 @@ +bugfixes: +- "docker_network - now returns warnings from docker daemon on network creation." +- "docker_swarm_service - now returns warnings from docker daemon on service creation." diff --git a/lib/ansible/module_utils/docker/common.py b/lib/ansible/module_utils/docker/common.py index be6dd994e9..3c5dacece9 100644 --- a/lib/ansible/module_utils/docker/common.py +++ b/lib/ansible/module_utils/docker/common.py @@ -24,6 +24,7 @@ from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule, env_fallback from ansible.module_utils.common._collections_compat import Mapping, Sequence +from ansible.module_utils.six import string_types from ansible.module_utils.six.moves.urllib.parse import urlparse from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE @@ -664,6 +665,15 @@ class AnsibleDockerClient(Client): def report_warnings(self, result, warnings_key=None): ''' Checks result of client operation for warnings, and if present, outputs them. + + warnings_key should be a list of keys used to crawl the result dictionary. + For example, if warnings_key == ['a', 'b'], the function will consider + result['a']['b'] if these keys exist. If the result is a non-empty string, it + will be reported as a warning. If the result is a list, every entry will be + reported as a warning. + + In most cases (if warnings are returned at all), warnings_key should be + ['Warnings'] or ['Warning']. The default value (if not specified) is ['Warnings']. ''' if warnings_key is None: warnings_key = ['Warnings'] @@ -674,6 +684,8 @@ class AnsibleDockerClient(Client): if isinstance(result, Sequence): for warning in result: self.module.warn('Docker warning: {0}'.format(warning)) + elif isinstance(result, string_types) and result: + self.module.warn('Docker warning: {0}'.format(result)) def compare_dict_allow_more_present(av, bv): diff --git a/lib/ansible/modules/cloud/docker/docker_network.py b/lib/ansible/modules/cloud/docker/docker_network.py index 1431020474..fb10e95f35 100644 --- a/lib/ansible/modules/cloud/docker/docker_network.py +++ b/lib/ansible/modules/cloud/docker/docker_network.py @@ -496,7 +496,7 @@ class DockerNetworkManager(object): if not self.check_mode: resp = self.client.create_network(self.parameters.network_name, **params) - + self.client.report_warnings(resp, ['Warning']) self.existing_network = self.client.get_network(id=resp['Id']) self.results['actions'].append("Created network %s with driver %s" % (self.parameters.network_name, self.parameters.driver)) self.results['changed'] = True diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index 9ef9d002e0..1ea3476122 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -2255,10 +2255,13 @@ class DockerServiceManager(object): name=name, **service_data ) + # Unfortunately, docker-py f***ed up and doesn't return the structure + # the corresponding API call returns, which would include a list of warnings. def create_service(self, name, service): service_data = service.build_docker_service(self.get_networks_names_ids()) - self.client.create_service(name=name, **service_data) + result = self.client.create_service(name=name, **service_data) + self.client.report_warnings(result, ['Warning']) def remove_service(self, name): self.client.remove_service(name)