简体   繁体   中英

How to append data obtained via Azure CLI in a Python list and dictionary?

I am using azure CLI to obtain the list of Virtual machine Instances in a virtual machine scale set (VMSS): "az vmss list-instances --resource-group xyzrg --name xyz" . I want to add the the names of different instances that is obtained as the output to a python list. How should I do that?

To strictly answer your question, CLI can return JSON, that you could parse. I guess based on your question that you sub-process the CLI from Python, so get the subprocess output and re-parse it as JSON.

That being said, I have to say that if this is your only need, that's over-complicated, and you'd better using the SDK: https://docs.microsoft.com/en-us/python/api/azure-mgmt-compute/azure.mgmt.compute.v2018_04_01.operations.virtualmachinescalesetsoperations?view=azure-python#list

You can even load the CLI credentials if your concern is an authentication problem: https://docs.microsoft.com/en-us/python/azure/python-sdk-azure-authenticate?view=azure-python#mgmt-auth-cli

This would be done in a few lines:

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')]

Just sayin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM