【问题标题】:Printing list of Virtual Machines打印虚拟机列表
【发布时间】:2022-01-16 09:57:49
【问题描述】:

我想在我的 azure 上列出我的虚拟机。此代码运行正常,但我以对象地址的形式获得输出。如何将此对象地址转换为可读信息。输出是<azure.mgmt.compute.v2019_03_01.models.virtual_machine_paged.VirtualMachinePaged object at 0x00000200D5C49E50>

from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials


Subscription_Id = "XXXXXX"
Tenant_Id = "XXXX"
Client_Id = "XXXX"
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()
print(vm_list)

【问题讨论】:

  • 如果它真的是列表,那么也许你应该使用for-loop 来分别显示每个元素?你也可以查看dir(vm_list) 看看它有什么功能。也许它具有提供更多细节的功能。
  • 现在我只有一个虚拟机,但无法获取。你能指导我语法吗
  • 我不知道语法是什么——我什至在 Azure 上都没有服务器。但首先我要做的是print( dir(vm_list) ) 来查看该对象中的所有功能和属性。最终我会使用help(vm_list)。如果您使用名称为 list_all() 的函数获得它,那么我会检查它是否像真实列表 - for item in vm_list: print(item)。要获得更多详细信息,我还将搜索此模块的文档或源代码。
  • 如果回答对您有帮助,请Accept it as an Answer,以便其他遇到相同问题的人可以找到此解决方案并解决他们的问题。

标签: python azure azure-virtual-machine azure-python-sdk


【解决方案1】:

VM list all 返回Paged values 的结果。因此,要打印所有 VM,您需要使用 by_pagenext

另一个问题是 <azure.common.credentials><ServicePrincipalCredentials> 没有 get_token ,因此在检索分页值时会在身份验证失败时出错。为避免这种情况,您可以使用与服务主体凭据相同的<azure.identity><ClientSecretCredentails>

我在我的环境中测试了将上述解决方案应用于您的代码,如下所示:

from azure.mgmt.compute import ComputeManagementClient
from azure.identity import ClientSecretCredential


Subscription_Id = "xxxx"
Tenant_Id = "xxxx"
Client_Id = "xxxxx"
Secret = "xxxxx"

credential = ClientSecretCredential(
    client_id=Client_Id,
    client_secret=Secret,
    tenant_id=Tenant_Id
)

compute_client = ComputeManagementClient(credential, Subscription_Id)

vm_list = compute_client.virtual_machines.list_all()
pageobject1=vm_list.by_page(continuation_token=None)
for page in pageobject1:
    for j in page:
        print(j)

输出:

注意:如果要展开network_profilediagnostics_profile em> 等,然后您可以将它们添加到相同的 for 循环中,如下所示:

vm_list = compute_client.virtual_machines.list_all()

pageobject1=vm_list.by_page(continuation_token=None)
for page in pageobject1:
    for j in page:
        network_profile=j.network_profile
        hardware_profile=j.hardware_profile
        print("VM Details : ", j)
        print("Network Profile : ", network_profile)
        print("Hardware Profile : " , hardware_profile)

输出:

【讨论】:

  • 我正面临来自您的代码的此错误,请指导我完成此错误。而且你的输出也没有显示你的虚拟机的名称它显示的对象地址如何解决这个File "c:\Users\USER\OneDrive\Desktop\try.py", line 19, in <module> pageobject1 = vm_list.by_page(continuation_token=None) AttributeError: 'VirtualMachinePaged' object has no attribute 'by_page'
  • @HassanTuri,让我们在聊天室继续吧:chat.stackoverflow.com/rooms/240087/chat-room
  • @HassanTuri ,请使用最新版本的 azure 软件包,即 azure-mgmt-compute 22.1.0azure-identity 1.6.0
  • and also your outputs are not shown the name of your vms it is showing the object address how to resolve this 我的输出显示了 VM 名称以及我突出显示的其他详细信息。
  • 先生,我无法使用需要 20 声望的聊天室。同样的错误我也更新了库。
猜你喜欢
  • 1970-01-01
  • 2012-06-11
  • 2013-07-12
  • 2013-04-03
  • 2020-09-08
  • 2013-04-29
  • 2012-05-31
  • 2017-03-24
  • 2012-03-13
相关资源
最近更新 更多