From f4081c2d97189e46ce2f8561c6014c043a2ffadc Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 23 Oct 2018 08:52:08 +0200 Subject: [PATCH] docker_container: warn if ipvX_address is used for networks but not supported by docker-py (#47395) * Only add parameters which are actually used. * Fail if ipvX_address is used when not supported. * Added changelog. (cherry picked from commit 4ffe3b14d4e90ee0daec7826db8361b4c71b1296) --- .../47395-docker_container-ipvX_address.yaml | 2 ++ .../modules/cloud/docker/docker_container.py | 20 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/47395-docker_container-ipvX_address.yaml diff --git a/changelogs/fragments/47395-docker_container-ipvX_address.yaml b/changelogs/fragments/47395-docker_container-ipvX_address.yaml new file mode 100644 index 00000000000..a4171772072 --- /dev/null +++ b/changelogs/fragments/47395-docker_container-ipvX_address.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - fail if ``ipv4_address`` or ``ipv6_address`` is used with a too old docker-py version." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index b42e8114cac..6460d5354ba 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1996,12 +1996,10 @@ class ContainerManager(DockerBaseClass): self.fail("Error disconnecting container from network %s - %s" % (diff['parameter']['name'], str(exc))) # connect to the network - params = dict( - ipv4_address=diff['parameter'].get('ipv4_address', None), - ipv6_address=diff['parameter'].get('ipv6_address', None), - links=diff['parameter'].get('links', None), - aliases=diff['parameter'].get('aliases', None) - ) + params = dict() + for para in ('ipv4_address', 'ipv6_address', 'links', 'aliases'): + if diff['parameter'].get(para): + params[para] = diff['parameter'][para] self.results['actions'].append(dict(added_to_network=diff['parameter']['name'], network_parameters=params)) if not self.check_mode: try: @@ -2210,6 +2208,16 @@ class AnsibleDockerClientContainer(AnsibleDockerClient): self.fail("docker or docker-py version is %s. Minimum version required is 2.3 to set cpuset_mems option. " "If you use the 'docker-py' module, you have to switch to the docker 'Python' package." % (docker_version,)) + ipvX_address_supported = LooseVersion(docker_version) >= LooseVersion('1.9') + if not ipvX_address_supported: + ipvX_address_used = False + for network in self.module.params.get("networks", []): + if 'ipv4_address' in network or 'ipv6_address' in network: + ipvX_address_used = True + if ipvX_address_used: + self.fail("docker or docker-py version is %s. Minimum version required is 1.9 to use " + "ipv4_address or ipv6_address in networks." % (docker_version,)) + self.HAS_INIT_OPT = init_supported self.HAS_UTS_MODE_OPT = uts_mode_supported self.HAS_BLKIO_WEIGHT_OPT = blkio_weight_supported