# Buggy code
def __init__(self, module):
    """
    :param module:
    """
    self.module = module
    self.name = self.module.params["name"]
    self.login_user = self.module.params["login_user"]
    self.login_password = self.module.params["login_password"]
    self.login_host = self.module.params["login_host"]
    self.login_port = self.module.params["login_port"]
    self.vhost = self.module.params["vhost"]
    self.destination = self.module.params["destination"]
    self.destination_type = (
        "q" if self.module.params["destination_type"] == "queue" else "e"
    )
    self.routing_key = self.module.params["routing_key"]
    self.arguments = self.module.params["arguments"]
    self.base_url = "http://{0}:{1}/api/bindings".format(
        self.login_host, self.login_port
    )
    self.url = "{0}/{1}/e/{2}/{3}/{4}/{5}".format(
        self.base_url,
        urllib_parse.quote(self.vhost, safe=""),
        urllib_parse.quote(self.name, safe=""),
        self.destination_type,
        urllib_parse.quote(self.destination, safe=""),
        urllib_parse.quote(self.routing_key),
    )
    self.result = {
        "changed": False,
        "name": self.module.params["name"],
    }
    self.authentication = (self.login_user, self.login_password)
    self.request = requests
    self.http_check_states = {
        200: True,
        404: False,
    }
    self.http_actionable_states = {
        201: True,
        204: True,
    }
    self.api_result = self.request.get(self.url, auth=self.authentication)


# 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 __init__(self, module):
    """
    :param module:
    """
    self.module = module
    self.name = self.module.params["name"]
    self.login_user = self.module.params["login_user"]
    self.login_password = self.module.params["login_password"]
    self.login_host = self.module.params["login_host"]
    self.login_port = self.module.params["login_port"]
    self.login_protocol = self.module.params["login_protocol"]
    self.vhost = self.module.params["vhost"]
    self.destination = self.module.params["destination"]
    self.destination_type = (
        "q" if self.module.params["destination_type"] == "queue" else "e"
    )
    self.routing_key = self.module.params["routing_key"]
    self.arguments = self.module.params["arguments"]
    self.verify = self.module.params["cacert"]
    self.cert = self.module.params["cert"]
    self.key = self.module.params["key"]
    self.base_url = "{0}://{1}:{2}/api/bindings".format(
        self.login_protocol, self.login_host, self.login_port
    )
    self.url = "{0}/{1}/e/{2}/{3}/{4}/{5}".format(
        self.base_url,
        urllib_parse.quote(self.vhost, safe=""),
        urllib_parse.quote(self.name, safe=""),
        self.destination_type,
        urllib_parse.quote(self.destination, safe=""),
        urllib_parse.quote(self.routing_key),
    )
    self.result = {
        "changed": False,
        "name": self.module.params["name"],
    }
    self.authentication = (self.login_user, self.login_password)
    self.request = requests
    self.http_check_states = {
        200: True,
        404: False,
    }
    self.http_actionable_states = {
        201: True,
        204: True,
    }
    self.api_result = self.request.get(self.url, auth=self.authentication)