【发布时间】:2019-11-19 01:35:26
【问题描述】:
我已使用以下代码从我的 Azure 帐户获取访问令牌。
一切正常,我已经拿到令牌了。
但是,如何使用此令牌列出所有在该订阅/资源组中运行的带有 Azure SDK for Python 的 VM?
我猜微软的文档有点混乱。
谢谢。
【问题讨论】:
标签: python python-3.x azure
我已使用以下代码从我的 Azure 帐户获取访问令牌。
一切正常,我已经拿到令牌了。
但是,如何使用此令牌列出所有在该订阅/资源组中运行的带有 Azure SDK for Python 的 VM?
我猜微软的文档有点混乱。
谢谢。
【问题讨论】:
标签: python python-3.x azure
您可以使用函数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 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('-----------------------------')
【讨论】:
我使用以下代码对其进行了排序。
使用来自 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"
}
【讨论】: