From 3fbeac4cf71b3288647c0027083cda9808c859f1 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 5 Nov 2018 20:32:01 +0100 Subject: [PATCH] [2.6] Fixes #33045: get existing containers in a network via inspect_network (Rebased #33048) (#47471) * Fix #33045: get existing network via inspect_network (Rebased #33048) (#43997) (cherry picked from commit 2939f68897ed85b530249e9918eb0a6a9ef35ad4) * Add changelog. --- .../fragments/43997-docker_network-inspect.yaml | 2 ++ lib/ansible/modules/cloud/docker/docker_network.py | 11 ++++------- 2 files changed, 6 insertions(+), 7 deletions(-) create mode 100644 changelogs/fragments/43997-docker_network-inspect.yaml diff --git a/changelogs/fragments/43997-docker_network-inspect.yaml b/changelogs/fragments/43997-docker_network-inspect.yaml new file mode 100644 index 00000000000..52d6f081f54 --- /dev/null +++ b/changelogs/fragments/43997-docker_network-inspect.yaml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_network - fixes idempotency issues (https://github.com/ansible/ansible/issues/33045) and name substring issue (https://github.com/ansible/ansible/issues/32926)." diff --git a/lib/ansible/modules/cloud/docker/docker_network.py b/lib/ansible/modules/cloud/docker/docker_network.py index 966ed7a3002..4180a00f823 100644 --- a/lib/ansible/modules/cloud/docker/docker_network.py +++ b/lib/ansible/modules/cloud/docker/docker_network.py @@ -161,6 +161,7 @@ from ansible.module_utils.docker_common import AnsibleDockerClient, DockerBaseCl try: from docker import utils + from docker.errors import NotFound if HAS_DOCKER_PY_2 or HAS_DOCKER_PY_3: from docker.types import IPAMPool, IPAMConfig except: @@ -215,14 +216,10 @@ class DockerNetworkManager(object): self.absent() def get_existing_network(self): - networks = self.client.networks(names=[self.parameters.network_name]) - # check if a user is trying to find network by its Id - if not networks: - networks = self.client.networks(ids=[self.parameters.network_name]) - if not networks: + try: + return self.client.inspect_network(self.parameters.network_name) + except NotFound: return None - else: - return networks[0] def has_different_config(self, net): '''