如何将通过azurecli获得的数据附加到Python列表和字典中?

2024-09-29 17:18:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用azurecli获取虚拟机规模集(vms)中的虚拟机实例列表:“az vms list Instances--resource group xyzrg--name xyz”。我想将作为输出的不同实例的名称添加到python列表中。我该怎么做?在


Tags: instances实例name名称列表groupresourcelist
1条回答
网友
1楼 · 发布于 2024-09-29 17:18:21

为了严格回答您的问题,CLI可以返回JSON,您可以解析它。我想根据您的问题,您将从Python子处理CLI,因此获取子进程输出并将其重新解析为JSON。在

话虽如此,我不得不说,如果这是你唯一的需要,那就太复杂了,你最好使用SDK: https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2018_04_01.operations.virtualmachinescalesetsoperations?view=azure-python#list

如果您担心的是身份验证问题,您甚至可以加载CLI凭据: https://docs.microsoft.com/en-us/python/azure/python-sdk-azure-authenticate?view=azure-python#mgmt-auth-cli

这可以用几行来完成:

from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.compute import ComputeManagementClient

client = get_client_from_cli_profile(ComputeManagementClient)
result = [vmss.name for vmss in client.virtual_machine_scale_sets.list('xyzrg', 'xyz')]

只是说说而已

相关问题 更多 >

    热门问题