# Buggy code
def __init__(self):
    self.module_arg_spec = dict(
        resource_group=dict(type="str", required=True),
        name=dict(type="str", required=True),
        state=dict(choices=["present", "absent"], default="present", type="str"),
        location=dict(type="str"),
        short_hostname=dict(type="str"),
        vm_size=dict(type="str", required=True),
        admin_username=dict(type="str"),
        admin_password=dict(type="str", no_log=True),
        ssh_password_enabled=dict(type="bool", default=True),
        ssh_public_keys=dict(type="list"),
        image=dict(type="dict"),
        storage_account_name=dict(type="str", aliases=["storage_account"]),
        storage_container_name=dict(
            type="str", aliases=["storage_container"], default="vhds"
        ),
        storage_blob_name=dict(type="str", aliases=["storage_blob"]),
        os_disk_caching=dict(
            type="str",
            aliases=["disk_caching"],
            choices=["ReadOnly", "ReadWrite"],
            default="ReadOnly",
        ),
        managed_disk_type=dict(type="str", choices=["Standard_LRS", "Premium_LRS"]),
        os_type=dict(type="str", choices=["Linux", "Windows"], default="Linux"),
        public_ip_allocation_method=dict(
            type="str",
            choices=["Dynamic", "Static"],
            default="Static",
            aliases=["public_ip_allocation"],
        ),
        open_ports=dict(type="list"),
        network_interface_names=dict(type="list", aliases=["network_interfaces"]),
        remove_on_absent=dict(type="list", default=["all"]),
        virtual_network_resource_group=dict(type="str"),
        virtual_network_name=dict(type="str", aliases=["virtual_network"]),
        subnet_name=dict(type="str", aliases=["subnet"]),
        allocated=dict(type="bool", default=True),
        restarted=dict(type="bool", default=False),
        started=dict(type="bool", default=True),
        data_disks=dict(type="list"),
    )

    self.resource_group = None
    self.name = None
    self.state = None
    self.location = None
    self.short_hostname = None
    self.vm_size = None
    self.admin_username = None
    self.admin_password = None
    self.ssh_password_enabled = None
    self.ssh_public_keys = None
    self.image = None
    self.storage_account_name = None
    self.storage_container_name = None
    self.storage_blob_name = None
    self.os_type = None
    self.os_disk_caching = None
    self.managed_disk_type = None
    self.network_interface_names = None
    self.remove_on_absent = set()
    self.tags = None
    self.force = None
    self.public_ip_allocation_method = None
    self.open_ports = None
    self.virtual_network_resource_group = None
    self.virtual_network_name = None
    self.subnet_name = None
    self.allocated = None
    self.restarted = None
    self.started = None
    self.differences = None
    self.data_disks = None

    self.results = dict(
        changed=False,
        actions=[],
        powerstate_change=None,
        ansible_facts=dict(azure_vm=None),
    )

    super(AzureRMVirtualMachine, self).__init__(
        derived_arg_spec=self.module_arg_spec, supports_check_mode=True
    )


# 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 __init__(self):
    self.module_arg_spec = dict(
        resource_group=dict(type="str", required=True),
        name=dict(type="str", required=True),
        state=dict(choices=["present", "absent"], default="present", type="str"),
        location=dict(type="str"),
        short_hostname=dict(type="str"),
        vm_size=dict(type="str"),
        admin_username=dict(type="str"),
        admin_password=dict(type="str", no_log=True),
        ssh_password_enabled=dict(type="bool", default=True),
        ssh_public_keys=dict(type="list"),
        image=dict(type="dict"),
        storage_account_name=dict(type="str", aliases=["storage_account"]),
        storage_container_name=dict(
            type="str", aliases=["storage_container"], default="vhds"
        ),
        storage_blob_name=dict(type="str", aliases=["storage_blob"]),
        os_disk_caching=dict(
            type="str",
            aliases=["disk_caching"],
            choices=["ReadOnly", "ReadWrite"],
            default="ReadOnly",
        ),
        managed_disk_type=dict(type="str", choices=["Standard_LRS", "Premium_LRS"]),
        os_type=dict(type="str", choices=["Linux", "Windows"], default="Linux"),
        public_ip_allocation_method=dict(
            type="str",
            choices=["Dynamic", "Static"],
            default="Static",
            aliases=["public_ip_allocation"],
        ),
        open_ports=dict(type="list"),
        network_interface_names=dict(type="list", aliases=["network_interfaces"]),
        remove_on_absent=dict(type="list", default=["all"]),
        virtual_network_resource_group=dict(type="str"),
        virtual_network_name=dict(type="str", aliases=["virtual_network"]),
        subnet_name=dict(type="str", aliases=["subnet"]),
        allocated=dict(type="bool", default=True),
        restarted=dict(type="bool", default=False),
        started=dict(type="bool", default=True),
        data_disks=dict(type="list"),
    )

    self.resource_group = None
    self.name = None
    self.state = None
    self.location = None
    self.short_hostname = None
    self.vm_size = None
    self.admin_username = None
    self.admin_password = None
    self.ssh_password_enabled = None
    self.ssh_public_keys = None
    self.image = None
    self.storage_account_name = None
    self.storage_container_name = None
    self.storage_blob_name = None
    self.os_type = None
    self.os_disk_caching = None
    self.managed_disk_type = None
    self.network_interface_names = None
    self.remove_on_absent = set()
    self.tags = None
    self.force = None
    self.public_ip_allocation_method = None
    self.open_ports = None
    self.virtual_network_resource_group = None
    self.virtual_network_name = None
    self.subnet_name = None
    self.allocated = None
    self.restarted = None
    self.started = None
    self.differences = None
    self.data_disks = None

    self.results = dict(
        changed=False,
        actions=[],
        powerstate_change=None,
        ansible_facts=dict(azure_vm=None),
    )

    super(AzureRMVirtualMachine, self).__init__(
        derived_arg_spec=self.module_arg_spec, supports_check_mode=True
    )