# Buggy code
def compare_listeners(
    connection, module, current_listeners, new_listeners, purge_listeners
):
    """
    Compare listeners and return listeners to add, listeners to modify and listeners to remove
    Listeners are compared based on port

    :param current_listeners:
    :param new_listeners:
    :param purge_listeners:
    :return:
    """

    listeners_to_modify = []
    listeners_to_delete = []

    # Check each current listener port to see if it's been passed to the module
    for current_listener in current_listeners:
        current_listener_passed_to_module = False
        for new_listener in new_listeners[:]:
            if current_listener["Port"] == new_listener["Port"]:
                current_listener_passed_to_module = True
                # Remove what we match so that what is left can be marked as 'to be added'
                new_listeners.remove(new_listener)
                modified_listener = compare_listener(current_listener, new_listener)
                if modified_listener:
                    modified_listener["Port"] = current_listener["Port"]
                    modified_listener["ListenerArn"] = current_listener["ListenerArn"]
                    listeners_to_modify.append(modified_listener)
                break

        # If the current listener was not matched against passed listeners and purge is True, mark for removal
        if not current_listener_passed_to_module and purge_listeners:
            listeners_to_delete.append(current_listener["ListenerArn"])

    listeners_to_add = new_listeners

    return listeners_to_add, listeners_to_modify, listeners_to_delete


# Traceback (botocore.exceptions.ParamValidationError)
task path: /Users/byi470/Workspaces/Ansible_Developer/roles/aws_create_loadbalancer/tasks/main.yml:17
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/basic.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/ec2.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/six/__init__.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/_text.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/parsing/__init__.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/pycompat24.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/cloud.py
Using module_utils file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/module_utils/six/_six.py
Using module file /Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/ansible/modules/cloud/amazon/elb_application_lb.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: byi470
<127.0.0.1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578 `" &amp;&amp; echo ansible-tmp-1502749039.44-189230135111578="` echo /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/tmpCIDeUf TO /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578/elb_application_lb.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578/ /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578/elb_application_lb.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/bin/python2.7 /Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578/elb_application_lb.py; rm -rf "/Users/byi470/.ansible/tmp/ansible-tmp-1502749039.44-189230135111578/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py", line 1010, in <module>
main()
File "/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py", line 1005, in main
create_or_update_elb(connection, connection_ec2, module)
File "/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py", line 900, in create_or_update_elb
listener_changed = create_or_update_elb_listeners(connection, module, elb)
File "/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py", line 721, in create_or_update_elb_listeners
connection.create_listener(**listener_to_add)
File "/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py", line 310, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py", line 573, in _make_api_call
api_params, operation_model, context=request_context)
File "/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py", line 628, in _convert_to_request_dict
api_params, operation_model)
File "/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/validate.py", line 291, in serialize_to_request
raise ParamValidationError(report=report.generate_report())
botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter Port, value: 8080, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>

failed: [localhost] (item={u'lb_listener_port': 443, u'app_prefix': u'potomac-test-dev-collector'}) => {
"changed": false,
"failed": true,
"item": {
"app_prefix": "potomac-test-dev-collector",
"lb_listener_port": 443,
},
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py\", line 1010, in <module>\n    main()\n  File \"/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py\", line 1005, in main\n    create_or_update_elb(connection, connection_ec2, module)\n  File \"/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py\", line 900, in create_or_update_elb\n    listener_changed = create_or_update_elb_listeners(connection, module, elb)\n  File \"/var/folders/k5/tnq7hf_s3fjc59sxdmh2hb7c8q_pwy/T/ansible_fFYd0r/ansible_module_elb_application_lb.py\", line 721, in create_or_update_elb_listeners\n    connection.create_listener(**listener_to_add)\n  File \"/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py\", line 310, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py\", line 573, in _make_api_call\n    api_params, operation_model, context=request_context)\n  File \"/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/client.py\", line 628, in _convert_to_request_dict\n    api_params, operation_model)\n  File \"/Users/byi470/Workspaces/Ansible_Developer/.ansible_virtualenv/lib/python2.7/site-packages/botocore/validate.py\", line 291, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\nbotocore.exceptions.ParamValidationError: Parameter validation failed:\nInvalid type for parameter Port, value: 443, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def compare_listeners(
    connection, module, current_listeners, new_listeners, purge_listeners
):
    """
    Compare listeners and return listeners to add, listeners to modify and listeners to remove
    Listeners are compared based on port

    :param current_listeners:
    :param new_listeners:
    :param purge_listeners:
    :return:
    """

    listeners_to_modify = []
    listeners_to_delete = []

    # Check each current listener port to see if it's been passed to the module
    for current_listener in current_listeners:
        current_listener_passed_to_module = False
        for new_listener in new_listeners[:]:
            new_listener["Port"] = int(new_listener["Port"])
            if current_listener["Port"] == new_listener["Port"]:
                current_listener_passed_to_module = True
                # Remove what we match so that what is left can be marked as 'to be added'
                new_listeners.remove(new_listener)
                modified_listener = compare_listener(current_listener, new_listener)
                if modified_listener:
                    modified_listener["Port"] = current_listener["Port"]
                    modified_listener["ListenerArn"] = current_listener["ListenerArn"]
                    listeners_to_modify.append(modified_listener)
                break

        # If the current listener was not matched against passed listeners and purge is True, mark for removal
        if not current_listener_passed_to_module and purge_listeners:
            listeners_to_delete.append(current_listener["ListenerArn"])

    listeners_to_add = new_listeners

    return listeners_to_add, listeners_to_modify, listeners_to_delete