From 2c473259cbc2c0dacc26724b72519bdfa2957972 Mon Sep 17 00:00:00 2001 From: Piotr Wojciechowski <23406016+WojciechowskiPiotr@users.noreply.github.com> Date: Tue, 19 Mar 2019 13:50:32 +0100 Subject: [PATCH] docker.swarm library docker bug workaround to provide correct swarm leader address (#53930) * Workaround for moby/moby#35437 bug for ansible.docker.swarm library to provide correct leader node IP address * Adding simple parser to separate IP address from port number for workaround * Adding simple parser to separate IP address from port number for workaround (inline) * Different split() method --- lib/ansible/module_utils/docker/swarm.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/ansible/module_utils/docker/swarm.py b/lib/ansible/module_utils/docker/swarm.py index eb627f490cb..9cdd52746da 100644 --- a/lib/ansible/module_utils/docker/swarm.py +++ b/lib/ansible/module_utils/docker/swarm.py @@ -4,6 +4,7 @@ import json from time import sleep +from re import split try: from docker.errors import APIError @@ -166,6 +167,17 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient): json_str = json.dumps(node_info, ensure_ascii=False) node_info = json.loads(json_str) + + if 'ManagerStatus' in node_info: + if node_info['ManagerStatus'].get('Leader'): + # This is workaround of bug in Docker when in some cases the Leader IP is 0.0.0.0 + # Check moby/moby#35437 for details + count_colons = node_info['ManagerStatus']['Addr'].count(":") + if count_colons == 1: + swarm_leader_ip = node_info['ManagerStatus']['Addr'].split(":", 1)[0] or node_info['Status']['Addr'] + else: + swarm_leader_ip = node_info['Status']['Addr'] + node_info['Status']['Addr'] = swarm_leader_ip return node_info def get_all_nodes_inspect(self):