You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python在Azure平台从托管磁盘创建虚拟机?求实现方案

Absolutely! Creating a virtual machine from an existing managed disk using Python is totally feasible with Azure's official management SDKs. I'll break down a practical, working solution for you below.

Prerequisites

First, make sure you have these in place:

  • The Azure Python SDK for Compute installed: Run pip install azure-mgmt-compute azure-identity
  • An active Azure subscription
  • The resource ID of your existing managed disk (you can find this in the Azure Portal under the disk's properties)
  • Proper authentication set up (either via Azure CLI login, service principal, or managed identity if running in Azure)
  • Existing network resources (virtual network and subnet) — the VM needs to connect to these; if you don't have them, you'll need to create them first (I'll include a note on that later)
Step-by-Step Implementation

Here's a complete code example that walks through the process:

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import (
    VirtualMachine,
    VirtualMachineIdentity,
    HardwareProfile,
    NetworkProfile,
    NetworkInterfaceReference,
    StorageProfile,
    OSDisk,
    ManagedDiskParameters
)

# --------------------------
# Configuration Variables
# --------------------------
SUBSCRIPTION_ID = "your-subscription-id"
RESOURCE_GROUP_NAME = "your-resource-group-name"
VM_NAME = "new-vm-from-disk"
LOCATION = "eastus"  # Match your disk's location
MANAGED_DISK_ID = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Compute/disks/your-existing-disk"
NETWORK_INTERFACE_ID = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Network/networkInterfaces/your-existing-nic"

# --------------------------
# Authenticate & Initialize Client
# --------------------------
credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, SUBSCRIPTION_ID)

# --------------------------
# Build VM Configuration
# --------------------------
vm_parameters = VirtualMachine(
    location=LOCATION,
    identity=VirtualMachineIdentity(type="SystemAssigned"),
    hardware_profile=HardwareProfile(vm_size="Standard_D2s_v3"),  # Choose a size compatible with your disk
    network_profile=NetworkProfile(
        network_interfaces=[
            NetworkInterfaceReference(id=NETWORK_INTERFACE_ID)
        ]
    ),
    storage_profile=StorageProfile(
        os_disk=OSDisk(
            os_type="Linux",  # Change to "Windows" if your disk is a Windows OS disk
            managed_disk=ManagedDiskParameters(id=MANAGED_DISK_ID),
            create_option="Attach"  # Critical: This tells Azure to attach the existing disk
        )
    )
)

# --------------------------
# Create the VM
# --------------------------
poller = compute_client.virtual_machines.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    VM_NAME,
    vm_parameters
)
vm_result = poller.result()

print(f"VM {vm_result.name} created successfully from managed disk!")
Key Notes to Keep in Mind
  • Disk Availability: The managed disk must be in an Unattached state before you can use it to create a VM. If it's attached to another VM, you'll need to detach it first.
  • OS Type Match: Make sure the os_type in the OSDisk configuration matches the actual OS on your managed disk (Linux/Windows). Mismatching this can cause boot failures.
  • Network Resources: If you don't have an existing network interface, you'll need to create one first (along with a virtual network and subnet). You can use the azure-mgmt-network SDK to automate that step too.
  • VM Size Compatibility: Choose a VM size that supports the disk's storage type (e.g., Premium SSD disks work with most VM sizes that support premium storage).
  • Permissions: The identity you're using (service principal, user account) needs permissions like Microsoft.Compute/virtualMachines/write and Microsoft.Compute/disks/read on the resource group.

If you run into specific issues (like boot errors or permission denials), feel free to share more details and I can help troubleshoot further!

内容的提问来源于stack exchange,提问作者cbhangale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:38:59