Azure SDK for Python 支持使用自定义映像创建 VM。
如果 Azure 门户的“我的图像”中已存在自定义图像,则可以使用 Azure Python SDK 使用存储上 VHD 图像的参数 OS_DISK_NAME&STORAGE_NAME 创建 VM。
Azure Python SDK API 封装了相同的函数 REST API。请参阅 REST API 文档“创建或更新虚拟机”https://msdn.microsoft.com/en-us/library/azure/mt163591.aspx,创建带有映像的 VM 只需要 osDisk 存储配置文件元素(参见下面的快照)。
因此示例代码仅部分修改(删除image_referencepart)如下:
# 4. Create the virtual machine
result = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
azure.mgmt.compute.VirtualMachine(
location=REGION,
name=VM_NAME,
os_profile=azure.mgmt.compute.OSProfile(
admin_username=ADMIN_USERNAME,
admin_password=ADMIN_PASSWORD,
computer_name=COMPUTER_NAME,
),
hardware_profile=azure.mgmt.compute.HardwareProfile(
virtual_machine_size=azure.mgmt.compute.VirtualMachineSizeTypes.standard_a0
),
network_profile=azure.mgmt.compute.NetworkProfile(
network_interfaces=[
azure.mgmt.compute.NetworkInterfaceReference(
reference_uri=nic_id,
),
],
),
storage_profile=azure.mgmt.compute.StorageProfile(
os_disk=azure.mgmt.compute.OSDisk(
caching=azure.mgmt.compute.CachingTypes.none,
create_option=azure.mgmt.compute.DiskCreateOptionTypes.from_image,
name=OS_DISK_NAME, // Your VHD name
virtual_hard_disk=azure.mgmt.compute.VirtualHardDisk(
uri='https://{0}.blob.core.windows.net/vhds/{1}.vhd'.format(
STORAGE_NAME, // your storage account name
OS_DISK_NAME, // Your VHD name
),
),
)
),
),
)