# Buggy code
def get_node_inspect(self, node_id=None, skip_missing=False):
    """
    Returns Swarm node info as in 'docker node inspect' command about single node

    :param skip_missing: if True then function will return None instead of failing the task
    :param node_id: node ID or name, if None then method will try to get node_id of host module run on
    :return:
        Single node information structure
    """

    if node_id is None:
        node_id = self.get_swarm_node_id()

    if node_id is None:
        self.fail("Failed to get node information.")

    try:
        node_info = self.inspect_node(node_id=node_id)
    except APIError as exc:
        if exc.status_code == 503:
            self.fail(
                "Cannot inspect node: To inspect node execute module on Swarm Manager"
            )
        if exc.status_code == 404:
            if skip_missing is False:
                self.fail("Error while reading from Swarm manager: %s" % to_native(exc))
            else:
                return None
    except Exception as exc:
        self.fail("Error inspecting swarm node: %s" % exc)

    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


# Traceback (UnboundLocalError)
TASK [swarm-worker : Ensure worker has joined the swarm] **********************************************************************************************************************************************************

fatal: [pgelinas-dev-ww0 -> pgelinas-dev-lm0.centralus.cloudapp.azure.com]: FAILED! => changed=false
attempts: 1
failed_when_result: true
module_stderr: |-
Shared connection to pgelinas-dev-lm0.centralus.cloudapp.azure.com closed.
module_stdout: |-
Traceback (most recent call last):
File "/home/azureuser/.ansible/tmp/ansible-tmp-1570731617.12-46436432626554/AnsiballZ_docker_node_info.py", line 114, in <module>
_ansiballz_main()
File "/home/azureuser/.ansible/tmp/ansible-tmp-1570731617.12-46436432626554/AnsiballZ_docker_node_info.py", line 106, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/home/azureuser/.ansible/tmp/ansible-tmp-1570731617.12-46436432626554/AnsiballZ_docker_node_info.py", line 49, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_docker_node_info_payload_nJH7PJ/__main__.py", line 158, in <module>
File "/tmp/ansible_docker_node_info_payload_nJH7PJ/__main__.py", line 145, in main
File "/tmp/ansible_docker_node_info_payload_nJH7PJ/__main__.py", line 123, in get_node_facts
File "/tmp/ansible_docker_node_info_payload_nJH7PJ/ansible_docker_node_info_payload.zip/ansible/module_utils/docker/swarm.py", line 170, in get_node_inspect
UnboundLocalError: local variable 'node_info' referenced before assignment
msg: |-
MODULE FAILURE
See stdout/stderr for the exact error
rc: 1

# Fixed code
def get_node_inspect(self, node_id=None, skip_missing=False):
    """
    Returns Swarm node info as in 'docker node inspect' command about single node

    :param skip_missing: if True then function will return None instead of failing the task
    :param node_id: node ID or name, if None then method will try to get node_id of host module run on
    :return:
        Single node information structure
    """

    if node_id is None:
        node_id = self.get_swarm_node_id()

    if node_id is None:
        self.fail("Failed to get node information.")

    try:
        node_info = self.inspect_node(node_id=node_id)
    except APIError as exc:
        if exc.status_code == 503:
            self.fail(
                "Cannot inspect node: To inspect node execute module on Swarm Manager"
            )
        if exc.status_code == 404:
            if skip_missing:
                return None
        self.fail("Error while reading from Swarm manager: %s" % to_native(exc))
    except Exception as exc:
        self.fail("Error inspecting swarm node: %s" % exc)

    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