![](/img/trans.png)
[英]How could I list details of only 1 Azure Virtual Machines using Python?
[英]How could I list Azure Virtual Machines using Python?
我使用下面的代碼從我的 Azure 帳戶獲取訪問令牌。
它工作正常,我已經得到了令牌。
但是,對於 Python,如何使用此令牌列出在該訂閱/資源組中運行的所有 VM,其中 Azure SDK?
我猜微軟文檔有點混亂。
謝謝。
您可以使用 function list_all() / list()獲取訂閱/資源組中的所有 VM,但這些響應不會向您顯示 VM 運行狀態。 所以你還需要function 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 - 這里是function ,其中包含有關 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 的此示例代碼獲取令牌:
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)
參數.json
{
"resource" : "https://management.azure.com",
"tenant" : "XXXXXXXXXXXXXXXXXXXX",
"authorityHostUrl" : "https://login.microsoftonline.com",
"clientId" : "XXXXXXXXXXXXXXXXXXXX",
"thumbprint" : "XXXXXXXXXXXXXXXXXXXX",
"privateKeyFile" : "XXXXXXXXXXXXXXXXXXXX.pem"
}
你可以試試這個代碼,
credentials = MSIAuthentication()
subscription_client = SubscriptionClient(credentials)
subscription = next(subscription_client.subscriptions.list())
subscription_id = subscription.subscription_id
compute_client = ComputeManagementClient(credentials, subscription_id)
resourceGroup = "myResourceGroup"
for vm in compute_client.virtual_machines.list(resourceGroup):
print(vm)
您需要列出 VM 實例和 ScaleSet 以獲得完整列表
list_all()/list() 方法未在 ScaleSet AFAIK 中列出 VM。 我找到的解決方案:
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.common.credentials import ServicePrincipalCredentials
subscription_id = 'XXXX'
tenant_id = 'XXXX'
client_id = 'XXXX'
client_secret = 'XXXX'
credentials = ServicePrincipalCredentials(tenant=tenant_id, client_id=client_id, secret=client_secret)
resource_client = ResourceManagementClient(credentials, subscription_id)
compute_client = ComputeManagementClient(credentials, subscription_id)
def list_vms_in_subscription():
group_list = resource_client.resource_groups.list()
for group in list(group_list):
list_vms_in_groups(group.name)
def list_vms_in_groups(group_name):
for resource in resource_client.resources.list_by_resource_group(group_name):
if resource.type == "Microsoft.Compute/virtualMachines":
print(resource.name)
if resource.type == "Microsoft.Compute/virtualMachineScaleSets":
vmss = compute_client.virtual_machine_scale_set_vms.list(group_name, resource.name)
for vm in vmss:
print(vm.name)
if __name__ == '__main__':
list_vms_in_subscription()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.