Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to list vms by specific Azure resource group

$
0
0

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 ComputeManagementClientfrom azure.mgmt.resource import ResourceManagementClientfrom azure.mgmt.resource import SubscriptionClientfrom azure.identity import DefaultAzureCredentialfrom azure.common import credentialsresource_group_name = "MyResourceGroup"# Below is a secure method of selecting the correct subscription without having to expose subID, tenantID, client_id and client_secretCREDENTIAL = 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_namecompute_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!


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>