我必须使用 API 或 python 包,它将为我提供所有 Azure 虚拟机实例的信息 表示 Azure 中存在的所有实例类型。
首先,使用 pip 安装 Azure Python SDK:
pip 安装 azure-sdk
接下来,导入必要的模块并使用 Azure 进行身份验证:
导入操作系统
从 azure.common.credentials 导入 ServicePrincipalCredentials
从 azure.mgmt.compute 导入 ComputeManagementClient
subscription_id = '我的订阅id'
client_id = 'my-client-id'
client_secret = 'my-client-secret'
tenant_id = 'my-tenant-id'`
credentials = ServicePrincipalCredentials( client_id=client_id, Secret=client_secret,tenant=tenant_id )
vm_list =compute_client.virtual_machines.list_all()
对于 vm_list 中的虚拟机:
打印(vm.name)
使用上面的代码,我只能看到我在订阅中创建的那些实例, 但我想要使用任何 API 或 python 包的所有实例类型及其信息。
最佳答案
如果您想获取所有 Azure 虚拟机,您可能应该使用资源图。
它将允许您获取您的服务主体有权访问的任何订阅的所有实例。
资源图请求应该类似于 资源 |其中类型 =~ 'microsoft.compute/virtualmachines'
关于python - 如何使用 API 或 python 包获取所有 azure vm 实例的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74861562/