0

我正在尝试编写一个 python 程序来帮助我管理我的虚拟机。当我在某个时候创建​​多个虚拟机时,会发生错误:

    async_vm_creation = compute_client.virtual_machines.create_or_update(
  File "C:\Python38\lib\site-packages\azure\mgmt\compute\v2019_03_01\operations\virtual_machines_operations.py", line 292, in create_or_update
    raw_result = self._create_or_update_initial(
  File "C:\Python38\lib\site-packages\azure\mgmt\compute\v2019_03_01\operations\virtual_machines_operations.py", line 252, in _create_or_update_initial   
    raise exp
msrestazure.azure_exceptions.CloudError: <unprintable CloudError object>

我一直在寻找类似的问题,但找不到适合我的解决方案!有没有人知道问题是什么?

这是我创建虚拟机的函数:当我想创建 VM 时,使用相应的凭据调用“createVM”方法

import os
import traceback

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import DiskCreateOption

from msrestazure.azure_exceptions import CloudError

# Azure Datacenter
LOCATION = 'westus'

# Resource Group
#resource_group = 'myResourceGroup6'

# Network
VNET_NAME = 'azure-sample-vnet'
SUBNET_NAME = 'azure-sample-subnet'

# VM
OS_DISK_NAME = 'azure-sample-osdisk'

IP_CONFIG_NAME = 'azure-sample-ip-config'
NIC_NAME = 'azure-sample-nic'
USERNAME = 'userlogin'
PASSWORD = 'Pa$$w0rd91'
#vmName = 'VmName'

VM_REFERENCE = {
    'linux': {
        'publisher': 'Canonical',
        'offer': 'UbuntuServer',
        'sku': '16.04.0-LTS',
        'version': 'latest'
    }
}

def run_example(credentials, subscription_id, resource_group, vmName):
    resource_client = ResourceManagementClient(credentials, subscription_id)
    compute_client = ComputeManagementClient(credentials, subscription_id)
    network_client = NetworkManagementClient(credentials, subscription_id)

    # Create Resource group
    print('\nCreate Resource Group')
    resource_client.resource_groups.create_or_update(
        resource_group, {'location': LOCATION})

    try:
        # Create a NIC
        nic = create_nic(network_client, resource_group)

        # Create Linux VM
        print('Creating Linux Virtual Machine')
        vm_parameters = create_vm_parameters(nic.id, VM_REFERENCE['linux'], vmName)
        async_vm_creation = compute_client.virtual_machines.create_or_update(
            resource_group, vmName, vm_parameters)
        async_vm_creation.wait()

    except CloudError:
        print('A VM operation failed:\n{}'.format(traceback.format_exc()))
    else:
        print('VM erfolgreich erstellt!')

def create_nic(network_client, resource_group):
    # Create VNet
    print('Create Vnet')
    async_vnet_creation = network_client.virtual_networks.create_or_update(
        resource_group,
        VNET_NAME,
        {
            'location': LOCATION,
            'address_space': {
                'address_prefixes': ['10.0.0.0/16']
            }
        }
    )
    async_vnet_creation.wait()

    # Create Subnet
    print('Create Subnet')
    async_subnet_creation = network_client.subnets.create_or_update(
        resource_group,
        VNET_NAME,
        SUBNET_NAME,
        {'address_prefix': '10.0.0.0/24'}
    )
    subnet_info = async_subnet_creation.result()

    # Create NIC
    print('Create NIC')
    async_nic_creation = network_client.network_interfaces.create_or_update(
        resource_group,
        NIC_NAME,
        {
            'location': LOCATION,
            'ip_configurations': [{
                'name': IP_CONFIG_NAME,
                'subnet': {
                    'id': subnet_info.id
                }
            }]
        }
    )
    return async_nic_creation.result()


def create_vm_parameters(nic_id, vm_reference, vmName):
    return {
        'location': LOCATION,
        'os_profile': {
            'computer_name': vmName,
            'admin_username': USERNAME,
            'admin_password': PASSWORD
        },
        'hardware_profile': {
            'vm_size': 'Standard_DS1_v2'
        },
        'storage_profile': {
            'image_reference': {
                'publisher': vm_reference['publisher'],
                'offer': vm_reference['offer'],
                'sku': vm_reference['sku'],
                'version': vm_reference['version']
            },
        },
        'network_profile': {
            'network_interfaces': [{
                'id': nic_id,
            }]
        },
    }


def createVM(credentials, subscription_id, vm_name):
    resourceGroup = "resourceGroup" + vm_name
    run_example(credentials, subscription_id, resourceGroup, vm_name)
4

0 回答 0