# Buggy code
def create_default_nic(self):
    """
    Create a default Network Interface <vm name>01. Requires an existing virtual network
    with one subnet. If NIC <vm name>01 exists, use it. Otherwise, create one.

    :return: NIC object
    """

    network_interface_name = self.name + "01"
    nic = None

    self.log("Create default NIC {0}".format(network_interface_name))
    self.log("Check to see if NIC {0} exists".format(network_interface_name))
    try:
        nic = self.network_client.network_interfaces.get(
            self.resource_group, network_interface_name
        )
    except CloudError:
        pass

    if nic:
        self.log("NIC {0} found.".format(network_interface_name))
        self.check_provisioning_state(nic)
        return nic

    self.log("NIC {0} does not exist.".format(network_interface_name))

    if self.virtual_network_name:
        try:
            self.network_client.virtual_networks.list(
                self.resource_group, self.virtual_network_name
            )
            virtual_network_name = self.virtual_network_name
        except Exception as exc:
            self.fail(
                "Error: fetching virtual network {0} - {1}".format(
                    self.virtual_network_name, str(exc)
                )
            )
    else:
        # Find a virtual network
        no_vnets_msg = (
            "Error: unable to find virtual network in resource group {0}. A virtual network "
            "with at least one subnet must exist in order to create a NIC for the virtual "
            "machine.".format(self.resource_group)
        )

        virtual_network_name = None
        try:
            vnets = self.network_client.virtual_networks.list(self.resource_group)
        except CloudError:
            self.log("cloud error!")
            self.fail(no_vnets_msg)

        for vnet in vnets:
            virtual_network_name = vnet.name
            self.log("vnet name: {0}".format(vnet.name))
            break

        if not virtual_network_name:
            self.fail(no_vnets_msg)

    if self.subnet_name:
        try:
            subnet = self.network_client.subnets.get(
                self.resource_group, virtual_network_name, self.subnet_name
            )
            subnet_id = subnet.id
        except Exception as exc:
            self.fail(
                "Error: fetching subnet {0} - {1}".format(self.subnet_name, str(exc))
            )
    else:
        no_subnets_msg = (
            "Error: unable to find a subnet in virtual network {0}. A virtual network "
            "with at least one subnet must exist in order to create a NIC for the virtual "
            "machine.".format(virtual_network_name)
        )

        subnet_id = None
        try:
            subnets = self.network_client.subnets.list(
                self.resource_group, virtual_network_name
            )
        except CloudError:
            self.fail(no_subnets_msg)

        for subnet in subnets:
            subnet_id = subnet.id
            self.log("subnet id: {0}".format(subnet_id))
            break

        if not subnet_id:
            self.fail(no_subnets_msg)

    self.results["actions"].append(
        "Created default public IP {0}".format(self.name + "01")
    )
    pip = self.create_default_pip(
        self.resource_group, self.location, self.name, self.public_ip_allocation_method
    )

    self.results["actions"].append(
        "Created default security group {0}".format(self.name + "01")
    )
    group = self.create_default_securitygroup(
        self.resource_group, self.location, self.name, self.os_type, self.open_ports
    )

    parameters = NetworkInterface(
        location=self.location,
        ip_configurations=[
            NetworkInterfaceIPConfiguration(
                private_ip_allocation_method="Dynamic",
            )
        ],
    )
    parameters.ip_configurations[0].subnet = Subnet(id=subnet_id)
    parameters.ip_configurations[0].name = "default"
    parameters.network_security_group = NetworkSecurityGroup(
        id=group.id, location=group.location, resource_guid=group.resource_guid
    )
    parameters.ip_configurations[0].public_ip_address = PublicIPAddress(
        id=pip.id, location=pip.location, resource_guid=pip.resource_guid
    )

    self.log("Creating NIC {0}".format(network_interface_name))
    self.log(self.serialize_obj(parameters, "NetworkInterface"), pretty_print=True)
    self.results["actions"].append("Created NIC {0}".format(network_interface_name))
    try:
        poller = self.network_client.network_interfaces.create_or_update(
            self.resource_group, network_interface_name, parameters
        )
        new_nic = self.get_poller_result(poller)
    except Exception as exc:
        self.fail(
            "Error creating network interface {0} - {1}".format(
                network_interface_name, str(exc)
            )
        )
    return new_nic


# Traceback (msrestazure.azure_exceptions.CloudError)
TASK [azure-vm-provision : Launch VM in Azure] *********************************
task path: /home/ec2-user/Ansible/roles/azure-vm-provision/tasks/windows.yml:5
Using module file /usr/local/lib/python2.7/site-packages/ansible/modules/core/cloud/azure/azure_rm_virtualmachine.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ec2-user
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826 `" &amp;&amp; echo ansible-tmp-1495723850.66-57642620789826="` echo $HOME/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826 `" ) &amp;&amp; sleep 0'
<127.0.0.1> PUT /tmp/tmpTQzyAQ TO /home/ec2-user/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826/azure_rm_virtualmachine.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ec2-user/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826/ /home/ec2-user/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826/azure_rm_virtualmachine.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2.7 /home/ec2-user/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826/azure_rm_virtualmachine.py; rm -rf "/home/ec2-user/.ansible/tmp/ansible-tmp-1495723850.66-57642620789826/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py", line 1305, in <module>
main()
File "/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py", line 1302, in main
AzureRMVirtualMachine()
File "/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py", line 548, in __init__
supports_check_mode=True)
File "/tmp/ansible_l_NvK6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 184, in __init__
File "/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py", line 716, in exec_module
default_nic = self.create_default_nic()
File "/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py", line 1256, in create_default_nic
for subnet in subnets:
File "/usr/lib/python2.7/dist-packages/msrest/paging.py", line 109, in __next__
self.advance_page()
File "/usr/lib/python2.7/dist-packages/msrest/paging.py", line 95, in advance_page
self._response = self._get_next(self.next_link)
File "/usr/lib/python2.7/dist-packages/azure/mgmt/network/operations/subnets_operations.py", line 350, in internal_paging
raise exp
msrestazure.azure_exceptions.CloudError: Azure Error: ResourceNotFound
Message: The Resource 'Microsoft.Network/virtualNetworks/subscriptions' under resource group '[azure_resource_group]' was not found.

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_name": "azure_rm_virtualmachine"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py\", line 1305, in <module>\n    main()\n  File \"/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py\", line 1302, in main\n    AzureRMVirtualMachine()\n  File \"/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py\", line 548, in __init__\n    supports_check_mode=True)\n  File \"/tmp/ansible_l_NvK6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 184, in __init__\n  File \"/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py\", line 716, in exec_module\n    default_nic = self.create_default_nic()\n  File \"/tmp/ansible_l_NvK6/ansible_module_azure_rm_virtualmachine.py\", line 1256, in create_default_nic\n    for subnet in subnets:\n  File \"/usr/lib/python2.7/dist-packages/msrest/paging.py\", line 109, in __next__\n    self.advance_page()\n  File \"/usr/lib/python2.7/dist-packages/msrest/paging.py\", line 95, in advance_page\n    self._response = self._get_next(self.next_link)\n  File \"/usr/lib/python2.7/dist-packages/azure/mgmt/network/operations/subnets_operations.py\", line 350, in internal_paging\n    raise exp\nmsrestazure.azure_exceptions.CloudError: Azure Error: ResourceNotFound\nMessage: The Resource 'Microsoft.Network/virtualNetworks/subscriptions' under resource group '[azure_resource_group]' was not found.\n",
"module_stdout": "",
"msg": "MODULE FAILURE"

# Fixed code
def create_default_nic(self):
    """
    Create a default Network Interface <vm name>01. Requires an existing virtual network
    with one subnet. If NIC <vm name>01 exists, use it. Otherwise, create one.

    :return: NIC object
    """

    network_interface_name = self.name + "01"
    nic = None

    self.log("Create default NIC {0}".format(network_interface_name))
    self.log("Check to see if NIC {0} exists".format(network_interface_name))
    try:
        nic = self.network_client.network_interfaces.get(
            self.resource_group, network_interface_name
        )
    except CloudError:
        pass

    if nic:
        self.log("NIC {0} found.".format(network_interface_name))
        self.check_provisioning_state(nic)
        return nic

    self.log("NIC {0} does not exist.".format(network_interface_name))

    virtual_network_resource_group = None
    if self.virtual_network_resource_group:
        virtual_network_resource_group = self.virtual_network_resource_group
    else:
        virtual_network_resource_group = self.resource_group

    if self.virtual_network_name:
        try:
            self.network_client.virtual_networks.list(
                virtual_network_resource_group, self.virtual_network_name
            )
            virtual_network_name = self.virtual_network_name
        except CloudError:
            self.fail(
                "Error: fetching virtual network {0} - {1}".format(
                    self.virtual_network_name, str(exc)
                )
            )

    else:
        # Find a virtual network
        no_vnets_msg = (
            "Error: unable to find virtual network in resource group {0}. A virtual network "
            "with at least one subnet must exist in order to create a NIC for the virtual "
            "machine.".format(self.resource_group)
        )

        virtual_network_name = None
        try:
            vnets = self.network_client.virtual_networks.list(self.resource_group)
        except CloudError:
            self.log("cloud error!")
            self.fail(no_vnets_msg)

        for vnet in vnets:
            virtual_network_name = vnet.name
            self.log("vnet name: {0}".format(vnet.name))
            break

        if not virtual_network_name:
            self.fail(no_vnets_msg)

    if self.subnet_name:
        try:
            subnet = self.network_client.subnets.get(
                self.resource_group, virtual_network_name, self.subnet_name
            )
            subnet_id = subnet.id
        except Exception as exc:
            self.fail(
                "Error: fetching subnet {0} - {1}".format(self.subnet_name, str(exc))
            )
    else:
        no_subnets_msg = (
            "Error: unable to find a subnet in virtual network {0}. A virtual network "
            "with at least one subnet must exist in order to create a NIC for the virtual "
            "machine.".format(virtual_network_name)
        )

        subnet_id = None
        try:
            subnets = self.network_client.subnets.list(
                virtual_network_resource_group, virtual_network_name
            )
        except CloudError:
            self.fail(no_subnets_msg)

        for subnet in subnets:
            subnet_id = subnet.id
            self.log("subnet id: {0}".format(subnet_id))
            break

        if not subnet_id:
            self.fail(no_subnets_msg)

    self.results["actions"].append(
        "Created default public IP {0}".format(self.name + "01")
    )
    pip = self.create_default_pip(
        self.resource_group, self.location, self.name, self.public_ip_allocation_method
    )

    self.results["actions"].append(
        "Created default security group {0}".format(self.name + "01")
    )
    group = self.create_default_securitygroup(
        self.resource_group, self.location, self.name, self.os_type, self.open_ports
    )

    parameters = NetworkInterface(
        location=self.location,
        ip_configurations=[
            NetworkInterfaceIPConfiguration(
                private_ip_allocation_method="Dynamic",
            )
        ],
    )
    parameters.ip_configurations[0].subnet = Subnet(id=subnet_id)
    parameters.ip_configurations[0].name = "default"
    parameters.network_security_group = NetworkSecurityGroup(
        id=group.id, location=group.location, resource_guid=group.resource_guid
    )
    parameters.ip_configurations[0].public_ip_address = PublicIPAddress(
        id=pip.id, location=pip.location, resource_guid=pip.resource_guid
    )

    self.log("Creating NIC {0}".format(network_interface_name))
    self.log(self.serialize_obj(parameters, "NetworkInterface"), pretty_print=True)
    self.results["actions"].append("Created NIC {0}".format(network_interface_name))
    try:
        poller = self.network_client.network_interfaces.create_or_update(
            self.resource_group, network_interface_name, parameters
        )
        new_nic = self.get_poller_result(poller)
    except Exception as exc:
        self.fail(
            "Error creating network interface {0} - {1}".format(
                network_interface_name, str(exc)
            )
        )
    return new_nic