【问题标题】:How could I list Azure Virtual Machines using Python?如何使用 Python 列出 Azure 虚拟机?
【发布时间】:2019-11-19 01:35:26
【问题描述】:

我已使用以下代码从我的 Azure 帐户获取访问令牌。

https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/certificate_credentials_sample.py

一切正常,我已经拿到令牌了。

但是,如何使用此令牌列出所有在该订阅/资源组中运行的带有 Azure SDK for Python 的 VM?

我猜微软的文档有点混乱。

谢谢。

【问题讨论】:

    标签: python python-3.x azure


    【解决方案1】:

    您可以使用函数list_all()/list() 获取订阅/资源组中的所有虚拟机,但这些响应不会向您显示虚拟机运行状态。所以还需要instance_view()这个函数来获取VM的运行状态。

    最后,列出在该订阅/资源组中运行的所有 VM 的示例代码如下:

    from azure.mgmt.compute import ComputeManagementClient
    from azure.common.credentials import ServicePrincipalCredentials
    
    
    Subscription_Id = "xxxxx"
    Tenant_Id = "xxxxx"
    Client_Id = "xxxxx"
    Secret = "xxxxx"
    
    credential = ServicePrincipalCredentials(
            client_id=Client_Id,
            secret=Secret,
            tenant=Tenant_Id
            )
    
    compute_client = ComputeManagementClient(credential, Subscription_Id)
    
    vm_list = compute_client.virtual_machines.list_all()
    # vm_list = compute_client.virtual_machines.list('resource_group_name')
    i= 0
    for vm in vm_list:
        array = vm.id.split("/")
        resource_group = array[4]
        vm_name = array[-1]
        statuses = compute_client.virtual_machines.instance_view(resource_group, vm_name).statuses
        status = len(statuses) >= 2 and statuses[1]
    
        if status and status.code == 'PowerState/running':
            print(vm_name)
    

    【讨论】:

    • 这很酷@Charles 然而,我没有 AuthN 的秘密,只有证书。
    • @tscomitre 好吧,使用不同的身份验证方式并不重要。这是获取正在运行的虚拟机的代码。您添加的答案仅获取虚拟机,并非所有虚拟机都处于运行状态。
    • 感谢这位伙伴,工作完美。我刚刚更改了 AuthN 方法并运行了您的代码。
    【解决方案2】:

    只是将信息添加到很好的答案@Charles Xu - here is 函数中包含有关 VM 的所有可能信息

    这是我的代码(感谢 Charles Xu - 你是我的英雄):

    from azure.mgmt.subscription import SubscriptionClient 
    from msrestazure.azure_active_directory import ServicePrincipalCredentials
    from azure.mgmt.compute import ComputeManagementClient
    
    credentials = ServicePrincipalCredentials('XXX', 'YYY', tenant='ZZZ')
    client = SubscriptionClient(credentials)
    subs = [sub.as_dict() for sub in client.subscriptions.list()]
    
    for subcription in subs:
        subscription_id = subcription.get('subscription_id')
        compute_client = ComputeManagementClient(credentials, subscription_id)
        vm_list = compute_client.virtual_machines.list_all()
        for vm_general in vm_list:
            general_view = vm_general.id.split("/")
            resource_group = general_view[4]
            vm_name = general_view[-1]
            vm = compute_client.virtual_machines.get(resource_group, vm_name, expand='instanceView')
    
            print("    osType: ", vm.storage_profile.os_disk.os_type.value)
            print("  name: ", vm.name)
            print("  type: ", vm.type)
            print("  location: ", vm.location)
            for stat in vm.instance_view.statuses:
                print("  code: ", stat.code)
            print('-----------------------------')
    

    【讨论】:

      【解决方案3】:

      我使用以下代码对其进行了排序。

      使用来自 MS 的示例代码获取 Token

      certificate_credentials_sample.py

      然后添加以下行:

      from msrestazure.azure_active_directory import AADTokenCredentials
      from azure.mgmt.compute import ComputeManagementClient
      
      client_id = 'XXXXXXXXXXXXX'
      
      credentials = AADTokenCredentials(token, client_id)
      
      compute_client = ComputeManagementClient(credentials, subscription_id)
      
      resourceGroup = 'XXXXXXXXXXXX'
      for vm in compute_client.virtual_machines.list(resourceGroup):
          print(vm)
      

      parameters.json

      {
          "resource" : "https://management.azure.com",
          "tenant" : "XXXXXXXXXXXXXXXXXXXX",
          "authorityHostUrl" : "https://login.microsoftonline.com",
          "clientId" : "XXXXXXXXXXXXXXXXXXXX",
          "thumbprint" : "XXXXXXXXXXXXXXXXXXXX",
          "privateKeyFile" : "XXXXXXXXXXXXXXXXXXXX.pem"
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-22
        • 1970-01-01
        • 1970-01-01
        • 2021-01-02
        • 2018-03-15
        • 1970-01-01
        • 2019-08-19
        • 2018-04-22
        相关资源
        最近更新 更多