# Buggy code
def delete_vm_storage(self, vhd_uris):
    for uri in vhd_uris:
        self.log("Extracting info from blob uri '{0}'".format(uri))
        try:
            blob_parts = extract_names_from_blob_uri(
                uri, self._cloud_environment.suffixes.storage_endpoint
            )
        except Exception as exc:
            self.fail("Error parsing blob URI {0}".format(str(exc)))
        storage_account_name = blob_parts["accountname"]
        container_name = blob_parts["containername"]
        blob_name = blob_parts["blobname"]

        blob_client = self.get_blob_client(self.resource_group, storage_account_name)

        self.log("Delete blob {0}:{1}".format(container_name, blob_name))
        self.results["actions"].append(
            "Deleted blob {0}:{1}".format(container_name, blob_name)
        )
        try:
            blob_client.delete_blob(container_name, blob_name)
        except Exception as exc:
            self.fail(
                "Error deleting blob {0}:{1} - {2}".format(
                    container_name, blob_name, str(exc)
                )
            )


# Traceback (AttributeError)
TASK [Clean up VM] **************************************************************************************************************************
task path: /home/elaurijssens/voyager-image-factory/playbooks/azure/vm_delete.yml:19
Using module file /usr/lib/python2.7/site-packages/ansible-2.4.0-py2.7.egg/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775 `" &amp;&amp; echo ansible-tmp-1505157460.96-218311556974775="` echo /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmpb9uhEf TO /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/ /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1524, in <module>
main()
File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1521, in main
AzureRMVirtualMachine()
File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 652, in __init__
supports_check_mode=True)
File "/tmp/ansible_QSCsG6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 255, in __init__
File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1074, in exec_module
self.delete_vm(vm)
File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1191, in delete_vm
vhd_uris.append(vm.storage_profile.os_disk.vhd.uri)
AttributeError: 'NoneType' object has no attribute 'uri'

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1524, in <module>\n    main()\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1521, in main\n    AzureRMVirtualMachine()\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 652, in __init__\n    supports_check_mode=True)\n  File \"/tmp/ansible_QSCsG6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 255, in __init__\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1074, in exec_module\n    self.delete_vm(vm)\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1191, in delete_vm\n    vhd_uris.append(vm.storage_profile.os_disk.vhd.uri)\nAttributeError: 'NoneType' object has no attribute 'uri'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

# Fixed code
def delete_vm_storage(self, vhd_uris):
    # FUTURE: figure out a cloud_env indepdendent way to delete these
    for uri in vhd_uris:
        self.log("Extracting info from blob uri '{0}'".format(uri))
        try:
            blob_parts = extract_names_from_blob_uri(
                uri, self._cloud_environment.suffixes.storage_endpoint
            )
        except Exception as exc:
            self.fail("Error parsing blob URI {0}".format(str(exc)))
        storage_account_name = blob_parts["accountname"]
        container_name = blob_parts["containername"]
        blob_name = blob_parts["blobname"]

        blob_client = self.get_blob_client(self.resource_group, storage_account_name)

        self.log("Delete blob {0}:{1}".format(container_name, blob_name))
        self.results["actions"].append(
            "Deleted blob {0}:{1}".format(container_name, blob_name)
        )
        try:
            blob_client.delete_blob(container_name, blob_name)
        except Exception as exc:
            self.fail(
                "Error deleting blob {0}:{1} - {2}".format(
                    container_name, blob_name, str(exc)
                )
            )