How to get All azure vm instances API or python package for using in my python code?

Kamlesh Bannagare 1 Reputation point
2022-12-19T05:09:08.477+00:00

I need an API or python package of all azure vm instances , so that I can use it in my python project programmatically.
So, I require configuration of all azure vm instance, those are available in azure .

Like there are many azure instances available like D2pls v5, D4pls v5, E96-24as v5, etc. means from A to Z type of instances.271910-screenshot-98.png

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
System Center Virtual Machine Manager
Azure Virtual Machine Scale Sets
Azure Virtual Machine Scale Sets
Azure compute resources that are used to create and manage groups of heterogeneous load-balanced virtual machines.
448 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ravi Kanth Koppala 3,391 Reputation points Microsoft Employee Moderator
    2022-12-19T05:33:10.757+00:00

    @Kamlesh Bannagare ,
    You can use the Azure Python SDK to get a list of all Azure virtual machine instances using an API or a Python package. The Azure Python SDK provides a set of libraries and tools that allow you to use the Azure resources from within your Python code.

    Here is an example of how you can use the Azure Python SDK to list all virtual machine instances in your Azure subscription:

    1. First, install the Azure Python SDK using pip: pip install azure-sdk
    2. Next, import the necessary modules and authenticate with Azure: import os
      from azure.common.credentials import ServicePrincipalCredentials
      from azure.mgmt.compute import ComputeManagementClient

      Replace with your Azure subscription ID

      subscription_id = 'your-subscription-id'

      Create a Service Principal and configure credentials

      client_id = 'your-client-id'
      client_secret = 'your-client-secret'
      tenant_id = 'your-tenant-id' credentials = ServicePrincipalCredentials(
      client_id=client_id,
      secret=client_secret,
      tenant=tenant_id
      )

      Create a Compute Management client

      compute_client = ComputeManagementClient(credentials, subscription_id)
    3. Finally, use the virtual_machines.list_all() method of the ComputeManagementClient to get a list of all virtual machine instances in your Azure subscription: vm_list = compute_client.virtual_machines.list_all() for vm in vm_list:
      print(vm.name)

    This will print the names of all virtual machine instances in your Azure subscription. You can also access other properties of the virtual machines, such as their resource group, location, and size, using the vm.resource_group and vm.hardware_profile attributes.

    For more information on using the Azure Python SDK to manage Azure resources, you can refer to the Azure SDK for Python documentation: https://azure-sdk-for-python.readthedocs.io/.

    ----------

    Please "Accept as Answer" and Upvote if any of the above helped so that it can help others in the community looking for remediation for similar issues.

    1 person found this answer helpful.

  2. Prrudram-MSFT 28,201 Reputation points Moderator
    2022-12-21T14:34:04.367+00:00

    Hi @Kamlesh Bannagare ,

    There is an Azure REST API to list all of the virtual machines in the specified subscription. For details see, https://learn.microsoft.com/en-us/rest/api/compute/virtual-machines/list-all?tabs=HTTP In order to list all the VMs and its details in the subscription, you would need Virtual Machine Contributor access under the subscription. Please ensure you have that role assigned to the user account you are using. To check the roles that are assigned to your account, follow below steps:

    1. Sign in to the Azure portal or Azure AD admin center.
    2. Select Azure Active Directory > Users > user name > Assigned roles.

    Example:

    272889-image.png

    I hope this helps! Let me know if you have any questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.