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
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:

run the command python get_vm_disks.py

Refer: