# Buggy code
def __update_swarm(self):
    try:
        self.inspect_swarm()
        version = self.swarm_info["Version"]["Index"]
        spec = self.swarm_info["Spec"]
        new_spec = self.__update_spec(spec)
        del spec["TaskDefaults"]
        if spec == new_spec:
            self.results["actions"].append("No modification")
            self.results["changed"] = False
            return
        if not self.check_mode:
            self.client.update_swarm(
                version=version,
                swarm_spec=new_spec,
                rotate_worker_token=self.parameters.rotate_worker_token,
                rotate_manager_token=self.parameters.rotate_manager_token,
            )
    except APIError as exc:
        self.client.fail("Can not update a Swarm Cluster: %s" % to_native(exc))
        return

    self.inspect_swarm()
    self.results["actions"].append("Swarm cluster updated")
    self.results["changed"] = True


# Traceback (nKeyError)
fatal: [node-01]: FAILED! => {"changed": false, "module_stderr": "Shared connection to node-01 closed.\r\n", "module_stdout": "/usr/lib/python2.7/site-packages/requests/__init__.py:91: RequestsDependencyWarning: urllib3 (1.24.1) or chardet (2.2.1) doesn't match a supported version!\r\n  RequestsDependencyWarning)\r\nTraceback (most recent call last):\r\n  File \"/root/.ansible/tmp/ansible-tmp-1548156057.5468266-261250085584743/AnsiballZ_docker_swarm.py\", line 113, in <module>\r\n    _ansiballz_main()\r\n  File \"/root/.ansible/tmp/ansible-tmp-1548156057.5468266-261250085584743/AnsiballZ_docker_swarm.py\", line 105, in _ansiballz_main\r\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\r\n  File \"/root/.ansible/tmp/ansible-tmp-1548156057.5468266-261250085584743/AnsiballZ_docker_swarm.py\", line 48, in invoke_module\r\n    imp.load_module('__main__', mod, module, MOD_DESC)\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 535, in <module>\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 530, in main\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 310, in __call__\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 333, in init_swarm\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 390, in __update_swarm\r\n  File \"/tmp/ansible_docker_swarm_payload_2QlvP8/__main__.py\", line 376, in __update_spec\r\nKeyError: 'Labels'\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

# Fixed code
def __update_swarm(self):
    try:
        self.inspect_swarm()
        version = self.swarm_info["Version"]["Index"]
        self.parameters.update_from_swarm_info(self.swarm_info)
        old_parameters = TaskParameters()
        old_parameters.update_from_swarm_info(self.swarm_info)
        self.parameters.compare_to_active(old_parameters, self.differences)
        if self.differences.empty:
            self.results["actions"].append("No modification")
            self.results["changed"] = False
            return
        self.parameters.update_parameters(self.client)
        if not self.check_mode:
            self.client.update_swarm(
                version=version,
                swarm_spec=self.parameters.spec,
                rotate_worker_token=self.parameters.rotate_worker_token,
                rotate_manager_token=self.parameters.rotate_manager_token,
            )
    except APIError as exc:
        self.client.fail("Can not update a Swarm Cluster: %s" % to_native(exc))
        return

    self.inspect_swarm()
    self.results["actions"].append("Swarm cluster updated")
    self.results["changed"] = True