diff --git a/changelogs/fragments/53262-docker_swarm_service-test_parameter_versions.yml b/changelogs/fragments/53262-docker_swarm_service-test_parameter_versions.yml new file mode 100644 index 00000000000..fc9a6735951 --- /dev/null +++ b/changelogs/fragments/53262-docker_swarm_service-test_parameter_versions.yml @@ -0,0 +1,3 @@ +bugfixes: +- "docker_swarm_service - don't crash when ``publish`` is not specified." +- "docker_swarm_service - do basic validation of ``publish`` option if specified (must be list of dicts)." diff --git a/lib/ansible/modules/cloud/docker/docker_swarm_service.py b/lib/ansible/modules/cloud/docker/docker_swarm_service.py index e5a6fdc72f2..b9fdf55c1a8 100644 --- a/lib/ansible/modules/cloud/docker/docker_swarm_service.py +++ b/lib/ansible/modules/cloud/docker/docker_swarm_service.py @@ -477,6 +477,7 @@ from ansible.module_utils.docker_common import docker_version from ansible.module_utils.basic import human_to_bytes from ansible.module_utils._text import to_text +from ansible.module_utils.common._collections_compat import Mapping try: from distutils.version import LooseVersion @@ -1055,7 +1056,9 @@ class DockerServiceManager(): msg=('%s parameter supported only with api_version>=%s' % (pv['param'], pv['min_version']))) - for publish_def in self.client.module.params.get('publish', []): + for publish_def in params['publish'] or []: + if not isinstance(publish_def, Mapping): + self.client.module.fail_json(msg='The publish option must be provided with a list of dicts!') if 'mode' in publish_def.keys(): if LooseVersion(self.client.version()['ApiVersion']) < LooseVersion('1.25'): self.client.module.fail_json(msg='publish.mode parameter supported only with api_version>=1.25')