# 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="str"),
        signing_ca_key=dict(type="str"),
        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),
        default_addr_pool=dict(type="list", elements="str"),
        subnet_size=dict(type="int"),
    )

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

    option_minimal_versions = dict(
        labels=dict(docker_py_version="2.6.0", docker_api_version="1.32"),
        signing_ca_cert=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        signing_ca_key=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        ca_force_rotate=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        autolock_managers=dict(docker_py_version="2.6.0"),
        log_driver=dict(docker_py_version="2.6.0"),
        remove_operation=dict(
            docker_py_version="2.4.0",
            detect_usage=_detect_remove_operation,
            usage_msg="remove swarm nodes",
        ),
        default_addr_pool=dict(docker_py_version="4.0.0", docker_api_version="1.39"),
        subnet_size=dict(docker_py_version="4.0.0", docker_api_version="1.39"),
    )

    client = AnsibleDockerSwarmClient(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=required_if,
        min_docker_version="1.10.0",
        min_docker_api_version="1.25",
        option_minimal_versions=option_minimal_versions,
    )

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

        SwarmManager(client, results)()
        client.module.exit_json(**results)
    except DockerException as e:
        client.fail(
            "An unexpected docker error occurred: {0}".format(e),
            exception=traceback.format_exc(),
        )


# Traceback (requests.exceptions.ConnectionError)
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))
fatal: [buster_local_docker]: FAILED! => changed=false
module_stderr: |-
Traceback (most recent call last):
File "/tmp/.ansible/tmp/ansible-tmp-1562335690.251731-79429560017149/AnsiballZ_docker_image.py", line 114, in <module>
_ansiballz_main()
File "/tmp/.ansible/tmp/ansible-tmp-1562335690.251731-79429560017149/AnsiballZ_docker_image.py", line 106, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/tmp/.ansible/tmp/ansible-tmp-1562335690.251731-79429560017149/AnsiballZ_docker_image.py", line 49, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/tmp/ansible_docker_image_payload_jIHMqS/__main__.py", line 926, in <module>
File "/tmp/ansible_docker_image_payload_jIHMqS/__main__.py", line 921, in main
File "/tmp/ansible_docker_image_payload_jIHMqS/__main__.py", line 476, in __init__
File "/tmp/ansible_docker_image_payload_jIHMqS/__main__.py", line 504, in present
File "/tmp/ansible_docker_image_payload_jIHMqS/__main__.py", line 731, in build_image
File "/usr/local/lib/python2.7/dist-packages/docker/api/build.py", line 269, in build
timeout=timeout,
File "/usr/local/lib/python2.7/dist-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/docker/api/client.py", line 226, in _post
return self.post(url, **self._set_request_timeout(kwargs))
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 581, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))
module_stdout: ''
msg: |-
MODULE FAILURE
See 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="str"),
        signing_ca_key=dict(type="str"),
        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),
        default_addr_pool=dict(type="list", elements="str"),
        subnet_size=dict(type="int"),
    )

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

    option_minimal_versions = dict(
        labels=dict(docker_py_version="2.6.0", docker_api_version="1.32"),
        signing_ca_cert=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        signing_ca_key=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        ca_force_rotate=dict(docker_py_version="2.6.0", docker_api_version="1.30"),
        autolock_managers=dict(docker_py_version="2.6.0"),
        log_driver=dict(docker_py_version="2.6.0"),
        remove_operation=dict(
            docker_py_version="2.4.0",
            detect_usage=_detect_remove_operation,
            usage_msg="remove swarm nodes",
        ),
        default_addr_pool=dict(docker_py_version="4.0.0", docker_api_version="1.39"),
        subnet_size=dict(docker_py_version="4.0.0", docker_api_version="1.39"),
    )

    client = AnsibleDockerSwarmClient(
        argument_spec=argument_spec,
        supports_check_mode=True,
        required_if=required_if,
        min_docker_version="1.10.0",
        min_docker_api_version="1.25",
        option_minimal_versions=option_minimal_versions,
    )

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

        SwarmManager(client, results)()
        client.module.exit_json(**results)
    except DockerException as e:
        client.fail(
            "An unexpected docker error occurred: {0}".format(e),
            exception=traceback.format_exc(),
        )
    except RequestException as e:
        client.fail(
            "An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}".format(
                e
            ),
            exception=traceback.format_exc(),
        )