pythonvirtual-machineazure-resource-group

How to list vms by specific Azure resource group


I am able to list VMs all within a Azure subscription.

But I am hoping to filter only listing VMs from a specific resource group.

Here is my python script:

from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource import SubscriptionClient
from azure.identity import DefaultAzureCredential
from azure.common import credentials

resource_group_name = "MyResourceGroup"
# Below is a secure method of selecting the correct subscription without having to expose subID, tenantID, client_id and client_secret
CREDENTIAL = DefaultAzureCredential(exclude_shared_token_cache_credential=True, exclude_visual_studio_code_credential=True, exclude_environment_credential=True, exclude_managed_identity_credential=True, 
                                        exclude_powershell_credential=True, exclude_interactive_browser_credential=True)
subscription_id = credentials.get_azure_cli_credentials()[1]
subscription_name = SubscriptionClient(CREDENTIAL).subscriptions.get(subscription_id).display_name

compute_client = ComputeManagementClient(CREDENTIAL, subscription_id)
resource_client = ResourceManagementClient(CREDENTIAL, subscription_id)

def list_vms_in_subscription():
    for group in resource_client.resource_groups.list():
        list_vms_in_groups(group.name)

def list_vms_in_groups(group_name):
    for vm in compute_client.virtual_machines.list(group_name):
        print(vm.name)

if __name__ == '__main__':
    list_vms_in_subscription()

Any help would be much appreciated!


Solution

  • I have below list of VM's in resource group named Identity-Resources like this:

    enter image description here

    To list VM's by specific Azure resource group, you can make use of below modified python code as @Abdul Niyas suggested:

    from azure.mgmt.compute import ComputeManagementClient
    from azure.mgmt.resource import ResourceManagementClient
    from azure.mgmt.resource import SubscriptionClient
    from azure.identity import DefaultAzureCredential
    from azure.common import credentials
    
    resource_group_name = "Identity-Resources"
    # Below is a secure method of selecting the correct subscription without having to expose subID, tenantID, client_id and client_secret
    CREDENTIAL = DefaultAzureCredential(exclude_shared_token_cache_credential=True, exclude_visual_studio_code_credential=True, exclude_environment_credential=True, exclude_managed_identity_credential=True, 
                                            exclude_powershell_credential=True, exclude_interactive_browser_credential=True)
    subscription_id = credentials.get_azure_cli_credentials()[1]
    subscription_name = SubscriptionClient(CREDENTIAL).subscriptions.get(subscription_id).display_name
    
    compute_client = ComputeManagementClient(CREDENTIAL, subscription_id)
    resource_client = ResourceManagementClient(CREDENTIAL, subscription_id)
    
    def list_vms_in_resource_group(group_name):
        for vm in compute_client.virtual_machines.list(group_name):
            print(vm.name)
    
    if __name__ == '__main__':
        list_vms_in_resource_group(resource_group_name)
    

    Response:

    enter image description here