# Buggy code
def list_snapshots(vm):
    result = {}
    snapshot = _get_vm_prop(vm, ("snapshot",))
    if not snapshot:
        return result
    if vm.snapshot is None:
        return result

    result["snapshots"] = list_snapshots_recursively(vm.snapshot.rootSnapshotList)
    current_snapref = vm.snapshot.currentSnapshot
    current_snap_obj = get_current_snap_obj(
        vm.snapshot.rootSnapshotList, current_snapref
    )
    result["current_snapshot"] = deserialize_snapshot_obj(current_snap_obj[0])

    return result


# Traceback (nIndexError)
{
"_ansible_parsed": false,
"exception": "Traceback (most recent call last):\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 409, in <module>\n    main()\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 397, in main\n    result = pyv.apply_snapshot_op(vm)\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 353, in apply_snapshot_op\n    result = {'changed': True, 'failed': False, 'results': list_snapshots(vm)}\n  File \"/tmp/ansible_dmAvX3/ansible_modlib.zip/ansible/module_utils/vmware.py\", line 414, in list_snapshots\nIndexError: list index out of range\n",
"changed": false,
"_ansible_item_label": {
"value": {
"vm_network": {
"00:50:56:ab:e3:58": {
"ipv4": [
"10.0.1.38"
],
"ipv6": [
"fe80::250:56ff:feab:e358"
]
}
},
"esxi_hostname": "sjcesx111.lab.ffs.com",
"power_state": "poweredOn",
"mac_address": [
"00:50:56:ab:e3:58",
"00:50:56:ab:2d:4a",
"00:50:56:ab:7d:80",
"00:50:56:ab:e6:17",
"00:50:56:ab:70:42"
],
"guest_fullname": "windows serv",
"ip_address": "10.0.1.38",
"uuid": "422bef20-a39f-f5f7-e858-882731505c27"
},
"key": "1138-wsss"
},
"_ansible_delegated_vars": {
"ansible_delegated_host": "localhost",
"ansible_host": "localhost"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 409, in <module>\n    main()\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 397, in main\n    result = pyv.apply_snapshot_op(vm)\n  File \"/tmp/ansible_dmAvX3/ansible_module_vmware_guest_snapshot.py\", line 353, in apply_snapshot_op\n    result = {'changed': True, 'failed': False, 'results': list_snapshots(vm)}\n  File \"/tmp/ansible_dmAvX3/ansible_modlib.zip/ansible/module_utils/vmware.py\", line 414, in list_snapshots\nIndexError: list index out of range\n",
"_ansible_item_result": true,
"module_stdout": "",
"item": {
"value": {
"vm_network": {
"00:50:56:ab:e3:58": {
"ipv4": [
"10.0.1.38"
],
"ipv6": [
"fe80::250:56ff:feab:e358"
]
}
},
"esxi_hostname": "sjcesx111.lab.ffs.com",
"power_state": "poweredOn",
"mac_address": [
"00:50:56:ab:e3:58",
"00:50:56:ab:2d:4a",
"00:50:56:ab:7d:80",
"00:50:56:ab:e6:17",
"00:50:56:ab:70:42"
],
"guest_fullname": "VMware ESXi 6.5",
"ip_address": "10.0.1.38",
"uuid": "422bef20-a39f-f5f7-e858-882731505c27"
},
"key": "1138-esx3"
},
"rc": 1,
"msg": "MODULE FAILURE",
"_ansible_ignore_errors": null,
"_ansible_no_log": false

# Fixed code
def list_snapshots(vm):
    result = {}
    snapshot = _get_vm_prop(vm, ("snapshot",))
    if not snapshot:
        return result
    if vm.snapshot is None:
        return result

    result["snapshots"] = list_snapshots_recursively(vm.snapshot.rootSnapshotList)
    current_snapref = vm.snapshot.currentSnapshot
    current_snap_obj = get_current_snap_obj(
        vm.snapshot.rootSnapshotList, current_snapref
    )
    if current_snap_obj:
        result["current_snapshot"] = deserialize_snapshot_obj(current_snap_obj[0])
    else:
        result["current_snapshot"] = dict()
    return result