# Buggy code
def get_volume_info(volume, region):
    attachment = volume["attachments"]

    volume_info = {
        "create_time": volume["create_time"],
        "id": volume["volume_id"],
        "encrypted": volume["encrypted"],
        "iops": volume["iops"] if "iops" in volume else None,
        "size": volume["size"],
        "snapshot_id": volume["snapshot_id"],
        "status": volume["state"],
        "type": volume["volume_type"],
        "zone": volume["availability_zone"],
        "region": region,
        "attachment_set": {
            "attach_time": attachment[0]["attach_time"]
            if len(attachment) > 0
            else None,
            "device": attachment[0]["device"] if len(attachment) > 0 else None,
            "instance_id": attachment[0]["instance_id"]
            if len(attachment) > 0
            else None,
            "status": attachment[0]["state"] if len(attachment) > 0 else None,
            "delete_on_termination": attachment[0]["delete_on_termination"]
            if len(attachment) > 0
            else None,
        },
        "tags": boto3_tag_list_to_ansible_dict(volume["tags"]),
    }

    return volume_info


# Traceback (KeyError)
TASK [ec2_vol_facts] *******************************************************************************************************************************************************
task path: /home/ph/git/zaplox-software-deploy/ph.yml:8
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/cloud/amazon/ec2_vol_facts.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ph
<localhost> EXEC /bin/sh -c 'AWS_REGION=eu-west-1 /usr/bin/python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "<stdin>", line 113, in <module>
File "<stdin>", line 105, in _ansiballz_main
File "<stdin>", line 48, in invoke_module
File "/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py", line 156, in <module>
File "/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py", line 152, in main
File "/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py", line 124, in list_ec2_volumes
File "/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py", line 96, in get_volume_info
KeyError: 'tags'

fatal: [poc1-web -> localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 113, in <module>\n  File \"<stdin>\", line 105, in _ansiballz_main\n  File \"<stdin>\", line 48, in invoke_module\n  File \"/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py\", line 156, in <module>\n  File \"/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py\", line 152, in main\n  File \"/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py\", line 124, in list_ec2_volumes\n  File \"/tmp/ansible_ec2_vol_facts_payload_F9x1Gs/__main__.py\", line 96, in get_volume_info\nKeyError: 'tags'\n",
"module_stdout": "",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}

# Fixed code
def get_volume_info(volume, region):
    attachment = volume["attachments"]

    volume_info = {
        "create_time": volume["create_time"],
        "id": volume["volume_id"],
        "encrypted": volume["encrypted"],
        "iops": volume["iops"] if "iops" in volume else None,
        "size": volume["size"],
        "snapshot_id": volume["snapshot_id"],
        "status": volume["state"],
        "type": volume["volume_type"],
        "zone": volume["availability_zone"],
        "region": region,
        "attachment_set": {
            "attach_time": attachment[0]["attach_time"]
            if len(attachment) > 0
            else None,
            "device": attachment[0]["device"] if len(attachment) > 0 else None,
            "instance_id": attachment[0]["instance_id"]
            if len(attachment) > 0
            else None,
            "status": attachment[0]["state"] if len(attachment) > 0 else None,
            "delete_on_termination": attachment[0]["delete_on_termination"]
            if len(attachment) > 0
            else None,
        },
        "tags": boto3_tag_list_to_ansible_dict(volume["tags"])
        if "tags" in volume
        else None,
    }

    return volume_info