From c89b7999392058e259aa18a64dfa83a0f7f149c8 Mon Sep 17 00:00:00 2001 From: Piotr Wojciechowski <23406016+WojciechowskiPiotr@users.noreply.github.com> Date: Sat, 16 Mar 2019 17:55:52 +0100 Subject: [PATCH] docker_swarm inventory plugin - Leader address Docker bug workaround (#53893) * * Workaround for Docker bug moby/moby#35437 when in some cases the Leader IP address is 0.0.0.0 instead of correct address * Changes in docs port 2375 to 2376 to avoid confusion wherever the TLS is mentioned as this is default port for TLS connections in docker * Imports optimization * Adjust documentation section to match ansibot parsing * Add comment why the workaround is required --- lib/ansible/plugins/inventory/docker_swarm.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ansible/plugins/inventory/docker_swarm.py b/lib/ansible/plugins/inventory/docker_swarm.py index 4a4414effe..e440056e21 100644 --- a/lib/ansible/plugins/inventory/docker_swarm.py +++ b/lib/ansible/plugins/inventory/docker_swarm.py @@ -11,7 +11,7 @@ DOCUMENTATION = ''' name: docker_swarm plugin_type: inventory version_added: '2.8' - authors: + author: - Stefan Heitmüller (@morph027) short_description: Ansible dynamic inventory plugin for Docker swarm nodes. requirements: @@ -69,12 +69,12 @@ host: tcp://my-docker-host:2375 # Example using remote docker with unverified TLS plugin: docker_swarm -host: tcp://my-docker-host:2375 +host: tcp://my-docker-host:2376 tls: yes # Example using remote docker with verified TLS and client certificate verification plugin: docker_swarm -host: tcp://my-docker-host:2375 +host: tcp://my-docker-host:2376 tls_verify: yes cacert_path: /somewhere/ca.pem key_path: /somewhere/key.pem @@ -98,10 +98,10 @@ keyed_groups: prefix: label ''' -from ansible.errors import AnsibleError, AnsibleParserError +from ansible.errors import AnsibleError from ansible.module_utils._text import to_native from ansible.plugins.inventory import BaseInventoryPlugin, Constructable -from ansible.release import __version__ +from ansible.parsing.utils.addresses import parse_address try: import docker @@ -181,6 +181,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable): self.inventory.set_variable(self.node_attrs['ID'], 'docker_swarm_node_attributes', self.node_attrs) if 'ManagerStatus' in self.node_attrs: if self.node_attrs['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 + self.inventory.set_variable(self.node_attrs['ID'], 'ansible_host', + parse_address(self.node_attrs['ManagerStatus']['Addr'])[0]) self.inventory.add_host(self.node_attrs['ID'], group='leader') # Use constructed if applicable strict = self.get_option('strict')