使用 Python SDK 从字符串推断 Azure VM 属性

Inferring Azure VM properties from string using Python SDK

给定一个字符串形式的 Azure VM 大小(例如“STANDARD_A4_v2”),我想以编程方式推断可用内存和 vCPU 的数量。我已经浏览 azure-mgmt-compute 但找不到我要找的东西。 我看到了this post using a ComputeManagementClient to iterate over all available VM sizes, but that's not what I need and furthermore in my case I only have access to Azure Batch credentials. Do I have to role my own (at least for vCPUs) following the naming conventions?

非常感谢,

安德烈亚斯

您应该能够使用 Azure 命令行并直接获得结果,例如 az vm List-sizes -l AustraliaEast

这给了我以下结果

您可以参考的文档 -- https://docs.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest#az-vm-list-sizes

注意您需要先使用'Az login'登录,然后才能执行上述命令

您看到的问题正是您所需要的。 virtual_machine_sizes只有一个功能,就是list。因此,您需要在列表中找到您的真实 VM 大小。例如:

compute_client = CompteManagementClient(credentials, subscription_id)
vmSizes = compute_client.virtual_machine_sizes.list(location)
for vmSize in vmSizes:
  if(vmSize.name == "STANDARD_A4_v2")
    print("number of vCPU: " + vmSize.number_of_cores)
    print("available memory: " + vmSize.memory_in_mb)

而命名约定是 Azure 如何定义 VM 大小的规则。您只需阅读它即可了解 VM 大小。