【问题标题】:Get a list of Azure OS Disks attached to VMs in specific subscription获取附加到特定订阅中 VM 的 Azure OS 磁盘列表
【发布时间】:2018-08-08 08:47:36
【问题描述】:

我正在获取特定订阅的所有资源组中附加到 Azure 中 VM 的操作系统磁盘列表。我找到了AZ utility 来获取 json 格式的列表。

使用下面的序列,我可以得到 json 格式的列表,有没有类似的方法可以使用任何 python 模块来实现?

az login
az account set --subscription <subscription>
az disk list

【问题讨论】:

    标签: python azure disk azure-cli


    【解决方案1】:

    是的,这是可能的。您可以使用方法list 来获取订阅中的磁盘。

    例如:

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.resource import ResourceManagementClient, SubscriptionClient
    
    # Tenant ID for your Azure Subscription
    TENANT_ID = ''
    
    # Your Service Principal App ID
    CLIENT = ''
    
    # Your Service Principal Password
    KEY = ''
    
    credentials = ServicePrincipalCredentials(
        client_id = CLIENT,
        secret = KEY,
        tenant = TENANT_ID
    )
    
    subscription_id = ''
    
    compute_client = ComputeManagementClient(credentials, subscription_id)
    
    disks = compute_client.disks.list()
    for disk in disks:
        print disk
    

    注意:它将返回您订阅中的所有磁盘。但有可能某些磁盘不是操作系统磁盘,它们可能是数据磁盘或未附加到 VM 的磁盘。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    相关资源
    最近更新 更多