I am trying to list Azure VMs using python code. could someone help me with this issue?
I have already tried to go through the code on the Microsoft website but it is not clear to me.
I am trying to list Azure VMs using python code. could someone help me with this issue?
I have already tried to go through the code on the Microsoft website but it is not clear to me.
Copyright © 2021 Jogjafile Inc.
First, you need to follow the section
Register your client application with Azure AD
of Azure offical documentAzure REST API Reference
to register an application with Azure AD on Azure portal for getting the required parametersclient_id
andsecret
for Authentication to list VMs.And, you continue to get the other required parameters
subscription_id
andtenant_id
.Then, to create a virtual environment in Python to install Azure SDK for Python via
pip install azure
or just install Azure Compute Management for Python viapip install azure-mgmt-compute
.Here is my sample code.
If just list VMs by resource group, to use function
list(resource_group_name, custom_headers=None, raw=False, **operation_config)
as the code below.Or you want to list all VMs in your subscription, to use function
list_all(custom_headers=None, raw=False, **operation_config)
as the code below.As references, there are two SO threads I think which may help for understanding deeper: How to get list of Azure VMs (non-classic/Resource Managed) using Java API and Is it anyway to get ftpsState of azure web app (Azure Function app) using Python SDK?.
Hope it helps.