如何使用Python在Azure中基于磁盘(VHD)创建Linux虚拟机?
使用Python在Azure创建基于自定义VHD的Linux虚拟机
没问题,我给你整理了一套完整的Python实现方案,直接就能复用,关键细节我都给你标注清楚了:
前置准备
首先得确保你装好了必要的Python包,打开终端跑这个命令:
pip install azure-mgmt-compute azure-mgmt-network azure-mgmt-resource azure-identity
另外,要确保你的本地环境已经完成Azure认证(比如用az login登录,或者配置服务主体,代码里用的DefaultAzureCredential会自动识别本地的认证信息)。
完整代码实现
下面是完整的代码,你只需要替换里面的占位符(比如<你的资源组名称>、<VHD的Blob存储URL>这些)就行:
from azure.identity import DefaultAzureCredential from azure.mgmt.compute import ComputeManagementClient from azure.mgmt.network import NetworkManagementClient from azure.mgmt.resource import ResourceManagementClient import uuid # 配置参数,替换成你自己的信息 SUBSCRIPTION_ID = "<你的Azure订阅ID>" RESOURCE_GROUP_NAME = "<你的资源组名称>" LOCATION = "eastus" # 换成你想用的区域 STORAGE_ACCOUNT_NAME = f"stor{uuid.uuid4().hex[:10]}" # 生成唯一存储账户名(可选,也可以自己指定) VHD_BLOB_URL = "<你的自定义VHD的Blob存储URL>" # 比如https://xxx.blob.core.windows.net/vhds/your-linux-disk.vhd VM_NAME = "my-custom-vhd-vm" VM_SIZE = "Standard_D2s_v3" # 初始化客户端 credential = DefaultAzureCredential() resource_client = ResourceManagementClient(credential, SUBSCRIPTION_ID) compute_client = ComputeManagementClient(credential, SUBSCRIPTION_ID) network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID) # 1. 创建资源组(如果不存在的话) print("正在创建资源组...") resource_client.resource_groups.create_or_update( RESOURCE_GROUP_NAME, {"location": LOCATION} ) # 2. 创建网络资源(虚拟网络、子网、公共IP、网络接口) print("正在创建网络资源...") # 虚拟网络 vnet = network_client.virtual_networks.begin_create_or_update( RESOURCE_GROUP_NAME, f"{VM_NAME}-vnet", { "location": LOCATION, "address_space": {"address_prefixes": ["10.0.0.0/16"]} } ).result() # 子网 subnet = network_client.subnets.begin_create_or_update( RESOURCE_GROUP_NAME, f"{VM_NAME}-vnet", f"{VM_NAME}-subnet", {"address_prefix": "10.0.0.0/24"} ).result() # 公共IP public_ip = network_client.public_ip_addresses.begin_create_or_update( RESOURCE_GROUP_NAME, f"{VM_NAME}-pip", { "location": LOCATION, "sku": {"name": "Standard"}, "public_ip_allocation_method": "Static" } ).result() # 网络接口 nic = network_client.network_interfaces.begin_create_or_update( RESOURCE_GROUP_NAME, f"{VM_NAME}-nic", { "location": LOCATION, "ip_configurations": [{ "name": "ipconfig1", "subnet": {"id": subnet.id}, "public_ip_address": {"id": public_ip.id} }] } ).result() # 3. 创建基于自定义VHD的托管磁盘 print("正在创建托管磁盘...") managed_disk = compute_client.disks.begin_create_or_update( RESOURCE_GROUP_NAME, f"{VM_NAME}-os-disk", { "location": LOCATION, "creation_data": { "create_option": "Import", "source_uri": VHD_BLOB_URL }, "disk_size_gb": 30, # 要大于等于原VHD的大小 "sku": {"name": "Standard_LRS"} # 可以换成Premium_LRS等 } ).result() # 4. 创建虚拟机 print("正在创建虚拟机...") vm_parameters = { "location": LOCATION, "hardware_profile": {"vm_size": VM_SIZE}, "network_profile": { "network_interfaces": [{"id": nic.id}] }, "storage_profile": { "os_disk": { "os_type": "Linux", "managed_disk": {"id": managed_disk.id}, "create_option": "Attach" } }, "os_profile": { "computer_name": VM_NAME, "admin_username": "<你的管理员用户名>", "admin_password": "<你的管理员密码>" # 如果用SSH密钥的话,换成linux_configuration # 用SSH密钥的话替换上面两行: # "admin_username": "azureuser", # "linux_configuration": { # "disable_password_authentication": True, # "ssh": { # "public_keys": [{ # "path": "/home/azureuser/.ssh/authorized_keys", # "key_data": "<你的SSH公钥内容>" # }] # } # } } } vm = compute_client.virtual_machines.begin_create_or_update( RESOURCE_GROUP_NAME, VM_NAME, vm_parameters ).result() print(f"虚拟机 {VM_NAME} 创建完成!ID: {vm.id}")
关键细节说明
- VHD的Blob URL:必须是Azure存储账户里Blob的完整URL,而且这个存储账户要和你创建VM的区域在同一个区域(或者启用了跨区域复制),同时要确保你的认证账号有读取这个Blob的权限。
- 磁盘大小:创建托管磁盘时指定的
disk_size_gb必须大于等于原VHD的实际大小,不然会创建失败。 - SSH密钥 vs 密码:代码里注释了SSH密钥的配置方式,推荐用SSH密钥代替密码,更安全。
- 客户端初始化:用
DefaultAzureCredential可以自动适配本地的Azure认证(比如az login的缓存、环境变量、服务主体等),不用硬编码密钥,更安全。
内容的提问来源于stack exchange,提问作者Piyush Doke
相关产品推荐
相关产品推荐

