0

How to get existing OS disk & multiple data disk id for multiple vms with for loop from Azure Python SDK

I tried different ways to get. But no luck

1
  • please share the code you tried so far @satishB Commented Mar 21 at 5:15

1 Answer 1

0

Getting existing OS disk & multiple data disk id for multiple vms

You can get the OS disk ID and multiple data disk IDs for multiple VMs using a for loop with the Azure Python SD is possible and can be achieved using the configuration share below.

Demo deployment(get_vm_disks.py):

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

subscription_id = "SubID"

credential = DefaultAzureCredential()
compute_client = ComputeManagementClient(credential, subscription_id)

resource_group_name = "bhaggi-rg"

vms = compute_client.virtual_machines.list(resource_group_name)

for vm in vms:
    vm_name = vm.name
    os_disk_id = vm.storage_profile.os_disk.managed_disk.id  # Get OS Disk ID

    
    data_disks = vm.storage_profile.data_disks
    data_disk_ids = [disk.managed_disk.id for disk in data_disks]

    print(f"VM Name: {vm_name}")
    print(f"OS Disk ID: {os_disk_id}")
    print(f"Data Disk IDs: {data_disk_ids}")
    print("-" * 50)

Deployment:

enter image description here

run the command python get_vm_disks.py

enter image description here

Refer:

https://learn.microsoft.com/en-us/azure/developer/python/sdk/examples/azure-sdk-samples-managed-disks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.