# Buggy code
def get_instance_info(instance):
    # Get groups
    groups = []
    for group in instance.groups:
        groups.append({"id": group.id, "name": group.name}.copy())

    # Get interfaces
    interfaces = []
    for interface in instance.interfaces:
        interfaces.append(
            {"id": interface.id, "mac_address": interface.mac_address}.copy()
        )

    # If an instance is terminated, sourceDestCheck is no longer returned
    try:
        source_dest_check = instance.sourceDestCheck
    except AttributeError:
        source_dest_check = None

    # Get block device mapping
    try:
        bdm_dict = []
        bdm = getattr(instance, "block_device_mapping")
        for device_name in bdm.keys():
            bdm_dict.append(
                {
                    "device_name": device_name,
                    "status": bdm[device_name].status,
                    "volume_id": bdm[device_name].volume_id,
                    "delete_on_termination": bdm[device_name].delete_on_termination,
                    "attach_time": bdm[device_name].attach_time,
                }
            )
    except AttributeError:
        pass

    instance_info = {
        "id": instance.id,
        "kernel": instance.kernel,
        "instance_profile": instance.instance_profile,
        "root_device_type": instance.root_device_type,
        "private_dns_name": instance.private_dns_name,
        "public_dns_name": instance.public_dns_name,
        "ebs_optimized": instance.ebs_optimized,
        "client_token": instance.client_token,
        "virtualization_type": instance.virtualization_type,
        "architecture": instance.architecture,
        "ramdisk": instance.ramdisk,
        "tags": instance.tags,
        "key_name": instance.key_name,
        "source_destination_check": source_dest_check,
        "image_id": instance.image_id,
        "groups": groups,
        "interfaces": interfaces,
        "spot_instance_request_id": instance.spot_instance_request_id,
        "requester_id": instance.requester_id,
        "monitoring_state": instance.monitoring_state,
        "placement": {
            "tenancy": instance._placement.tenancy,
            "zone": instance._placement.zone,
        },
        "ami_launch_index": instance.ami_launch_index,
        "launch_time": instance.launch_time,
        "hypervisor": instance.hypervisor,
        "region": instance.region.name,
        "persistent": instance.persistent,
        "private_ip_address": instance.private_ip_address,
        "public_ip_address": instance.ip_address,
        "state": instance._state.name,
        "vpc_id": instance.vpc_id,
        "block_device_mapping": bdm_dict,
    }

    return instance_info


# Traceback (TypeError)
PLAYBOOK: test.yml ****************************************************************************************************************************************************************************************
1 plays in test.yml

PLAY [AWS Deploy] *****************************************************************************************************************************************************************************************
META: ran handlers

TASK [Get matching instance list to add] ******************************************************************************************************************************************************************
task path: /Users/jmorgan/git/sovereign/ansible/test.yml:45
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/basic.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/ec2.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/_text.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/pycompat24.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/six/__init__.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/six/_six.py
Using module_utils file /Users/jmorgan/tmp/ansible/lib/ansible/module_utils/cloud.py
Using module file /Users/jmorgan/tmp/ansible/lib/ansible/modules/cloud/amazon/ec2_remote_facts.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: jmorgan
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /tmp/ansible-$USER/tmp/ansible-tmp-1498028256.33-39237385536896 `" &amp;&amp; echo ansible-tmp-1498028256.33-39237385536896="` echo /tmp/ansible-$USER/tmp/ansible-tmp-1498028256.33-39237385536896 `" ) &amp;&amp; sleep 0'
<localhost> PUT /var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/tmpKt4n4_ TO /tmp/ansible-jmorgan/tmp/ansible-tmp-1498028256.33-39237385536896/ec2_remote_facts.py
<localhost> EXEC /bin/sh -c 'chmod u+x /tmp/ansible-jmorgan/tmp/ansible-tmp-1498028256.33-39237385536896/ /tmp/ansible-jmorgan/tmp/ansible-tmp-1498028256.33-39237385536896/ec2_remote_facts.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/env python /tmp/ansible-jmorgan/tmp/ansible-tmp-1498028256.33-39237385536896/ec2_remote_facts.py; rm -rf "/tmp/ansible-jmorgan/tmp/ansible-tmp-1498028256.33-39237385536896/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py", line 194, in <module>
main()
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py", line 190, in main
list_ec2_instances(connection, module)
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py", line 163, in list_ec2_instances
module.exit_json(instances=instance_dict_array)
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py", line 2125, in exit_json
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py", line 2115, in _return_formatted
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py", line 506, in remove_values
File "/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py", line 472, in _remove_values_conditions
TypeError: __init__() takes at least 2 arguments (1 given)

fatal: [localhost -> localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py\", line 194, in <module>\n    main()\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py\", line 190, in main\n    list_ec2_instances(connection, module)\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_module_ec2_remote_facts.py\", line 163, in list_ec2_instances\n    module.exit_json(instances=instance_dict_array)\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py\", line 2125, in exit_json\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py\", line 2115, in _return_formatted\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py\", line 506, in remove_values\n  File \"/var/folders/yt/1w3x85v95qd3kt50624tzgww0000gn/T/ansible_C43B8G/ansible_modlib.zip/ansible/module_utils/basic.py\", line 472, in _remove_values_conditions\nTypeError: __init__() takes at least 2 arguments (1 given)\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def get_instance_info(instance):
    # Get groups
    groups = []
    for group in instance.groups:
        groups.append({"id": group.id, "name": group.name}.copy())

    # Get interfaces
    interfaces = []
    for interface in instance.interfaces:
        interfaces.append(
            {"id": interface.id, "mac_address": interface.mac_address}.copy()
        )

    # If an instance is terminated, sourceDestCheck is no longer returned
    try:
        source_dest_check = instance.sourceDestCheck
    except AttributeError:
        source_dest_check = None

    # Get block device mapping
    try:
        bdm_dict = []
        bdm = getattr(instance, "block_device_mapping")
        for device_name in bdm.keys():
            bdm_dict.append(
                {
                    "device_name": device_name,
                    "status": bdm[device_name].status,
                    "volume_id": bdm[device_name].volume_id,
                    "delete_on_termination": bdm[device_name].delete_on_termination,
                    "attach_time": bdm[device_name].attach_time,
                }
            )
    except AttributeError:
        pass

    instance_info = {
        "id": instance.id,
        "kernel": instance.kernel,
        "instance_profile": dict(instance.instance_profile),
        "root_device_type": instance.root_device_type,
        "private_dns_name": instance.private_dns_name,
        "public_dns_name": instance.public_dns_name,
        "ebs_optimized": instance.ebs_optimized,
        "client_token": instance.client_token,
        "virtualization_type": instance.virtualization_type,
        "architecture": instance.architecture,
        "ramdisk": instance.ramdisk,
        "tags": instance.tags,
        "key_name": instance.key_name,
        "source_destination_check": source_dest_check,
        "image_id": instance.image_id,
        "groups": groups,
        "interfaces": interfaces,
        "spot_instance_request_id": instance.spot_instance_request_id,
        "requester_id": instance.requester_id,
        "monitoring_state": instance.monitoring_state,
        "placement": {
            "tenancy": instance._placement.tenancy,
            "zone": instance._placement.zone,
        },
        "ami_launch_index": instance.ami_launch_index,
        "launch_time": instance.launch_time,
        "hypervisor": instance.hypervisor,
        "region": instance.region.name,
        "persistent": instance.persistent,
        "private_ip_address": instance.private_ip_address,
        "public_ip_address": instance.ip_address,
        "state": instance._state.name,
        "vpc_id": instance.vpc_id,
        "block_device_mapping": bdm_dict,
    }

    return instance_info