根据我的测试,您在vm_parameters
. 请更新managed_disk_parameters
到managed_disk
. 有关详细信息,请参阅https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2019_03_01.models.osdisk?view=azure-python。
例如:
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
from haikunator import Haikunator
haikunator = Haikunator()
AZURE_TENANT_ID= ''
AZURE_CLIENT_ID=''
AZURE_CLIENT_SECRET=''
AZURE_SUBSCRIPTION_ID=''
credentials = ServicePrincipalCredentials(client_id=AZURE_CLIENT_ID,secret=AZURE_CLIENT_SECRET,tenant=AZURE_TENANT_ID)
resource_client = ResourceManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
compute_client = ComputeManagementClient(credentials,AZURE_SUBSCRIPTION_ID)
network_client = NetworkManagementClient(credentials, AZURE_SUBSCRIPTION_ID)
GROUP_NAME='allenR'
LOCATION='eastus'
# Network
VNET_NAME = 'azure-sample-vnet'
SUBNET_NAME = 'azure-sample-subnet'
print('\nCreate Vnet')
async_vnet_creation = network_client.virtual_networks.create_or_update(
GROUP_NAME,
VNET_NAME,
{
'location': LOCATION,
'address_space': {
'address_prefixes': ['10.0.0.0/16']
}
}
)
async_vnet_creation.wait()
# Create Subnet
print('\nCreate Subnet')
async_subnet_creation = network_client.subnets.create_or_update(
GROUP_NAME,
VNET_NAME,
SUBNET_NAME,
{'address_prefix': '10.0.0.0/24'}
)
subnet_info = async_subnet_creation.result()
# Create NIC
print('\nCreate NIC')
async_nic_creation = network_client.network_interfaces.create_or_update(
GROUP_NAME,
'test19191',
{
'location': LOCATION,
'ip_configurations': [{
'name': 'test19191-ip',
'subnet': {
'id': subnet_info.id
}
}]
}
)
nic = async_nic_creation.result()
print(nic.id)
vm_parameters = {
'location': 'eastus',
'os_profile': {
'computer_name': 'jimtest120yue',
'admin_username': 'jimtest',
'admin_password': 'Password0123!',
#'custom_data': startup_script
},
'hardware_profile': {
'vm_size': 'Standard_B1ls'
},
'storage_profile': {
'image_reference': {
'publisher': 'Canonical',
'offer': 'UbuntuServer',
'sku': '16.04.0-LTS',
'version': 'latest'
},
'os_disk': {
'caching': 'ReadWrite',
'name' : 'jimtest120yue_disk',
'create_option': 'FromImage',
'disk_size_gb': 30,
'os_type': 'Linux',
'managed_disk': {
'storage_account_type': 'Standard_LRS'
}
}
},
'network_profile': {
'network_interfaces': [{
'id': nic.id
}]
},
'tags': {
'expiration_date': 'expirationdatehere'
}
}
async_vm_creation=compute_client.virtual_machines.create_or_update('allenR','jimtest120yue',vm_parameters)
print(async_vm_creation.result())
disk = compute_client.disks.get('allenR','jimtest120yue_disk')
print(disk.sku)