# Buggy code
def main():
    argument_spec = dict(
        advertise_addr=dict(type="str"),
        state=dict(
            type="str",
            default="present",
            choices=["present", "join", "absent", "remove", "inspect"],
        ),
        force=dict(type="bool", default=False),
        listen_addr=dict(type="str", default="0.0.0.0:2377"),
        remote_addrs=dict(type="list", elements="str"),
        join_token=dict(type="str"),
        snapshot_interval=dict(type="int"),
        task_history_retention_limit=dict(type="int"),
        keep_old_snapshots=dict(type="int"),
        log_entries_for_slow_followers=dict(type="int"),
        heartbeat_tick=dict(type="int"),
        election_tick=dict(type="int"),
        dispatcher_heartbeat_period=dict(type="int"),
        node_cert_expiry=dict(type="int"),
        name=dict(type="str"),
        labels=dict(type="dict"),
        signing_ca_cert=dict(type="path"),
        signing_ca_key=dict(type="path"),
        ca_force_rotate=dict(type="int"),
        autolock_managers=dict(type="bool"),
        node_id=dict(type="str"),
        rotate_worker_token=dict(type="bool", default=False),
        rotate_manager_token=dict(type="bool", default=False),
    )

    required_if = [
        ("state", "join", ["advertise_addr", "remote_addrs", "join_token"]),
        ("state", "remove", ["node_id"]),
    ]

    option_minimal_versions = dict(
        signing_ca_cert=dict(docker_api_version="1.30"),
        signing_ca_key=dict(docker_api_version="1.30"),
        ca_force_rotate=dict(docker_api_version="1.30"),
    )

    client = AnsibleDockerClient(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=required_if,
        min_docker_version="2.6.0",
        min_docker_api_version="1.25",
        option_minimal_versions=option_minimal_versions,
    )

    results = dict(changed=False, result="", actions=[])

    SwarmManager(client, results)()
    client.module.exit_json(**results)


# 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 main():
    argument_spec = dict(
        advertise_addr=dict(type="str"),
        state=dict(
            type="str",
            default="present",
            choices=["present", "join", "absent", "remove", "inspect"],
        ),
        force=dict(type="bool", default=False),
        listen_addr=dict(type="str", default="0.0.0.0:2377"),
        remote_addrs=dict(type="list", elements="str"),
        join_token=dict(type="str"),
        snapshot_interval=dict(type="int"),
        task_history_retention_limit=dict(type="int"),
        keep_old_snapshots=dict(type="int"),
        log_entries_for_slow_followers=dict(type="int"),
        heartbeat_tick=dict(type="int"),
        election_tick=dict(type="int"),
        dispatcher_heartbeat_period=dict(type="int"),
        node_cert_expiry=dict(type="int"),
        name=dict(type="str"),
        labels=dict(type="dict"),
        signing_ca_cert=dict(type="path"),
        signing_ca_key=dict(type="path"),
        ca_force_rotate=dict(type="int"),
        autolock_managers=dict(type="bool"),
        node_id=dict(type="str"),
        rotate_worker_token=dict(type="bool", default=False),
        rotate_manager_token=dict(type="bool", default=False),
    )

    required_if = [
        ("state", "join", ["advertise_addr", "remote_addrs", "join_token"]),
        ("state", "remove", ["node_id"]),
    ]

    option_minimal_versions = dict(
        labels=dict(docker_api_version="1.32"),
        signing_ca_cert=dict(docker_api_version="1.30"),
        signing_ca_key=dict(docker_api_version="1.30"),
        ca_force_rotate=dict(docker_api_version="1.30"),
    )

    client = AnsibleDockerClient(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=required_if,
        min_docker_version="2.6.0",
        min_docker_api_version="1.25",
        option_minimal_versions=option_minimal_versions,
    )

    results = dict(changed=False, result="", actions=[])

    SwarmManager(client, results)()
    client.module.exit_json(**results)