# Buggy code
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default="present", choices=["present", "absent"], type="str"),
            name=dict(required=True, type="str"),
            login_user=dict(default="guest", type="str"),
            login_password=dict(default="guest", type="str", no_log=True),
            login_host=dict(default="localhost", type="str"),
            login_port=dict(default="15672", type="str"),
            vhost=dict(default="/", type="str"),
            durable=dict(default=True, type="bool"),
            auto_delete=dict(default=False, type="bool"),
            message_ttl=dict(default=None, type="int"),
            auto_expires=dict(default=None, type="int"),
            max_length=dict(default=None, type="int"),
            dead_letter_exchange=dict(default=None, type="str"),
            dead_letter_routing_key=dict(default=None, type="str"),
            arguments=dict(default=dict(), type="dict"),
            max_priority=dict(default=None, type="int"),
        ),
        supports_check_mode=True,
    )

    url = "http://%s:%s/api/queues/%s/%s" % (
        module.params["login_host"],
        module.params["login_port"],
        urllib_parse.quote(module.params["vhost"], ""),
        module.params["name"],
    )

    if not HAS_REQUESTS:
        module.fail_json(
            msg="requests library is required for this module. To install, use `pip install requests`"
        )

    result = dict(changed=False, name=module.params["name"])

    # Check if queue already exists
    r = requests.get(
        url, auth=(module.params["login_user"], module.params["login_password"])
    )

    if r.status_code == 200:
        queue_exists = True
        response = r.json()
    elif r.status_code == 404:
        queue_exists = False
        response = r.text
    else:
        module.fail_json(
            msg="Invalid response from RESTAPI when trying to check if queue exists",
            details=r.text,
        )

    if module.params["state"] == "present":
        change_required = not queue_exists
    else:
        change_required = queue_exists

    # Check if attributes change on existing queue
    if (
        not change_required
        and r.status_code == 200
        and module.params["state"] == "present"
    ):
        if not (
            response["durable"] == module.params["durable"]
            and response["auto_delete"] == module.params["auto_delete"]
            and (
                (
                    "x-message-ttl" in response["arguments"]
                    and response["arguments"]["x-message-ttl"]
                    == module.params["message_ttl"]
                )
                or (
                    "x-message-ttl" not in response["arguments"]
                    and module.params["message_ttl"] is None
                )
            )
            and (
                (
                    "x-expires" in response["arguments"]
                    and response["arguments"]["x-expires"]
                    == module.params["auto_expires"]
                )
                or (
                    "x-expires" not in response["arguments"]
                    and module.params["auto_expires"] is None
                )
            )
            and (
                (
                    "x-max-length" in response["arguments"]
                    and response["arguments"]["x-max-length"]
                    == module.params["max_length"]
                )
                or (
                    "x-max-length" not in response["arguments"]
                    and module.params["max_length"] is None
                )
            )
            and (
                (
                    "x-dead-letter-exchange" in response["arguments"]
                    and response["arguments"]["x-dead-letter-exchange"]
                    == module.params["dead_letter_exchange"]
                )
                or (
                    "x-dead-letter-exchange" not in response["arguments"]
                    and module.params["dead_letter_exchange"] is None
                )
            )
            and (
                (
                    "x-dead-letter-routing-key" in response["arguments"]
                    and response["arguments"]["x-dead-letter-routing-key"]
                    == module.params["dead_letter_routing_key"]
                )
                or (
                    "x-dead-letter-routing-key" not in response["arguments"]
                    and module.params["dead_letter_routing_key"] is None
                )
            )
            and (
                (
                    "x-max-priority" in response["arguments"]
                    and response["arguments"]["x-max-priority"]
                    == module.params["max_priority"]
                )
                or (
                    "x-max-priority" not in response["arguments"]
                    and module.params["max_priority"] is None
                )
            )
        ):
            module.fail_json(
                msg="RabbitMQ RESTAPI doesn't support attribute changes for existing queues",
            )

    # Copy parameters to arguments as used by RabbitMQ
    for k, v in {
        "message_ttl": "x-message-ttl",
        "auto_expires": "x-expires",
        "max_length": "x-max-length",
        "dead_letter_exchange": "x-dead-letter-exchange",
        "dead_letter_routing_key": "x-dead-letter-routing-key",
        "max_priority": "x-max-priority",
    }.items():
        if module.params[k] is not None:
            module.params["arguments"][v] = module.params[k]

    # Exit if check_mode
    if module.check_mode:
        result["changed"] = change_required
        result["details"] = response
        result["arguments"] = module.params["arguments"]
        module.exit_json(**result)

    # Do changes
    if change_required:
        if module.params["state"] == "present":
            r = requests.put(
                url,
                auth=(module.params["login_user"], module.params["login_password"]),
                headers={"content-type": "application/json"},
                data=json.dumps(
                    {
                        "durable": module.params["durable"],
                        "auto_delete": module.params["auto_delete"],
                        "arguments": module.params["arguments"],
                    }
                ),
            )
        elif module.params["state"] == "absent":
            r = requests.delete(
                url, auth=(module.params["login_user"], module.params["login_password"])
            )

        # RabbitMQ 3.6.7 changed this response code from 204 to 201
        if r.status_code == 204 or r.status_code == 201:
            result["changed"] = True
            module.exit_json(**result)
        else:
            module.fail_json(
                msg="Error creating queue", status=r.status_code, details=r.text
            )

    else:
        result["changed"] = False
        module.exit_json(**result)


# Traceback (requests.exceptions.ConnectionError)
Using module file /home/redacted/Ansible/ansible/lib/ansible/modules/messaging/rabbitmq_queue.py
<172.16.191.87> ESTABLISH SSH CONNECTION FOR USER: automation
<172.16.191.87> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=automation -o ConnectTimeout=10 -o ControlPath=/home/redacted/.ansible/cp/999765f0fe 172.16.191.87 '/bin/sh -c '"'"'sudo -H -S -n -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-cfjgpsspmnwpsvntxvjablneyhyiyfvr; redacted/pyenvs/ansible/bin/python'"'"'"'"'"'"'"'"' &amp;&amp; sleep 0'"'"''
<172.16.191.87> (1, '', 'Traceback (most recent call last):\n  File "/tmp/ansible_qDVLbU/ansible_module_rabbitmq_queue.py", line 277, in <module>\n    main()\n  File "/tmp/ansible_qDVLbU/ansible_module_rabbitmq_queue.py", line 167, in main\n    r = requests.get( url, auth=(module.params[\'login_user\'],module.params[\'login_password\']))\n  File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/api.py", line 70, in get\n    return request(\'get\', url, params=params, **kwargs)\n  File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/api.py", line 56, in request\n    return session.request(method=method, url=url, **kwargs)\n  File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request\n    resp = self.send(prep, **send_kwargs)\n  File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send\n    r = adapter.send(request, **kwargs)\n  File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/adapters.py", line 487, in send\n    raise ConnectionError(e, request=request)\nrequests.exceptions.ConnectionError: HTTPConnectionPool(host=\'testhost.some.domain\', port=15671): Max retries exceeded with url: /api/queues/%2Fhec%2Ftest/recording.upload.processing (Caused by NewConnectionError(\'<requests.packages.urllib3.connection.HTTPConnection object at 0x7fca0cc12310>: Failed to establish a new connection: [Errno 111] Connection refused\',))\n')
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_qDVLbU/ansible_module_rabbitmq_queue.py", line 277, in <module>
main()
File "/tmp/ansible_qDVLbU/ansible_module_rabbitmq_queue.py", line 167, in main
r = requests.get( url, auth=(module.params['login_user'],module.params['login_password']))
File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/api.py", line 70, in get
return request('get', url, params=params, **kwargs)
File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "redacted/pyenvs/ansible/local/lib/python2.7/site-packages/requests/adapters.py", line 487, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='testhost.some.domain', port=15671): Max retries exceeded with url: /api/queues/%2Ffubar/testq (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7fca0cc12310>: Failed to establish a new connection: [Errno 111] Connection refused',))

# Fixed code
def main():
    argument_spec = rabbitmq_argument_spec()
    argument_spec.update(
        dict(
            state=dict(default="present", choices=["present", "absent"], type="str"),
            name=dict(required=True, type="str"),
            durable=dict(default=True, type="bool"),
            auto_delete=dict(default=False, type="bool"),
            message_ttl=dict(default=None, type="int"),
            auto_expires=dict(default=None, type="int"),
            max_length=dict(default=None, type="int"),
            dead_letter_exchange=dict(default=None, type="str"),
            dead_letter_routing_key=dict(default=None, type="str"),
            arguments=dict(default=dict(), type="dict"),
            max_priority=dict(default=None, type="int"),
        )
    )
    module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)

    url = "%s://%s:%s/api/queues/%s/%s" % (
        module.params["login_protocol"],
        module.params["login_host"],
        module.params["login_port"],
        urllib_parse.quote(module.params["vhost"], ""),
        module.params["name"],
    )

    if not HAS_REQUESTS:
        module.fail_json(
            msg="requests library is required for this module. To install, use `pip install requests`"
        )

    result = dict(changed=False, name=module.params["name"])

    # Check if queue already exists
    r = requests.get(
        url,
        auth=(module.params["login_user"], module.params["login_password"]),
        verify=module.params["cacert"],
        cert=(module.params["cert"], module.params["key"]),
    )

    if r.status_code == 200:
        queue_exists = True
        response = r.json()
    elif r.status_code == 404:
        queue_exists = False
        response = r.text
    else:
        module.fail_json(
            msg="Invalid response from RESTAPI when trying to check if queue exists",
            details=r.text,
        )

    if module.params["state"] == "present":
        change_required = not queue_exists
    else:
        change_required = queue_exists

    # Check if attributes change on existing queue
    if (
        not change_required
        and r.status_code == 200
        and module.params["state"] == "present"
    ):
        if not (
            response["durable"] == module.params["durable"]
            and response["auto_delete"] == module.params["auto_delete"]
            and (
                (
                    "x-message-ttl" in response["arguments"]
                    and response["arguments"]["x-message-ttl"]
                    == module.params["message_ttl"]
                )
                or (
                    "x-message-ttl" not in response["arguments"]
                    and module.params["message_ttl"] is None
                )
            )
            and (
                (
                    "x-expires" in response["arguments"]
                    and response["arguments"]["x-expires"]
                    == module.params["auto_expires"]
                )
                or (
                    "x-expires" not in response["arguments"]
                    and module.params["auto_expires"] is None
                )
            )
            and (
                (
                    "x-max-length" in response["arguments"]
                    and response["arguments"]["x-max-length"]
                    == module.params["max_length"]
                )
                or (
                    "x-max-length" not in response["arguments"]
                    and module.params["max_length"] is None
                )
            )
            and (
                (
                    "x-dead-letter-exchange" in response["arguments"]
                    and response["arguments"]["x-dead-letter-exchange"]
                    == module.params["dead_letter_exchange"]
                )
                or (
                    "x-dead-letter-exchange" not in response["arguments"]
                    and module.params["dead_letter_exchange"] is None
                )
            )
            and (
                (
                    "x-dead-letter-routing-key" in response["arguments"]
                    and response["arguments"]["x-dead-letter-routing-key"]
                    == module.params["dead_letter_routing_key"]
                )
                or (
                    "x-dead-letter-routing-key" not in response["arguments"]
                    and module.params["dead_letter_routing_key"] is None
                )
            )
            and (
                (
                    "x-max-priority" in response["arguments"]
                    and response["arguments"]["x-max-priority"]
                    == module.params["max_priority"]
                )
                or (
                    "x-max-priority" not in response["arguments"]
                    and module.params["max_priority"] is None
                )
            )
        ):
            module.fail_json(
                msg="RabbitMQ RESTAPI doesn't support attribute changes for existing queues",
            )

    # Copy parameters to arguments as used by RabbitMQ
    for k, v in {
        "message_ttl": "x-message-ttl",
        "auto_expires": "x-expires",
        "max_length": "x-max-length",
        "dead_letter_exchange": "x-dead-letter-exchange",
        "dead_letter_routing_key": "x-dead-letter-routing-key",
        "max_priority": "x-max-priority",
    }.items():
        if module.params[k] is not None:
            module.params["arguments"][v] = module.params[k]

    # Exit if check_mode
    if module.check_mode:
        result["changed"] = change_required
        result["details"] = response
        result["arguments"] = module.params["arguments"]
        module.exit_json(**result)

    # Do changes
    if change_required:
        if module.params["state"] == "present":
            r = requests.put(
                url,
                auth=(module.params["login_user"], module.params["login_password"]),
                headers={"content-type": "application/json"},
                data=json.dumps(
                    {
                        "durable": module.params["durable"],
                        "auto_delete": module.params["auto_delete"],
                        "arguments": module.params["arguments"],
                    }
                ),
                verify=module.params["cacert"],
                cert=(module.params["cert"], module.params["key"]),
            )
        elif module.params["state"] == "absent":
            r = requests.delete(
                url,
                auth=(module.params["login_user"], module.params["login_password"]),
                verify=module.params["cacert"],
                cert=(module.params["cert"], module.params["key"]),
            )

        # RabbitMQ 3.6.7 changed this response code from 204 to 201
        if r.status_code == 204 or r.status_code == 201:
            result["changed"] = True
            module.exit_json(**result)
        else:
            module.fail_json(
                msg="Error creating queue", status=r.status_code, details=r.text
            )

    else:
        module.exit_json(changed=False, name=module.params["name"])