0

I have written this code to list down the Azure Virtual Machine. Now I want to print all the details related to the OS disks such as OS, disk size, encryption settings, and other details related to Azure Virtual Machine

from azure.mgmt.resource import ResourceManagementClient
from azure.identity import ClientSecretCredential

Subscription_Id = "XXXX"
Tenant_Id = "XXXX"
Client_Id = "XXXX"
Secret = "XXXX"

credential = ClientSecretCredential(
    client_id=Client_Id,
    client_secret=Secret,
    tenant_id=Tenant_Id
)
resource_client = ResourceManagementClient(
    credential=credential, subscription_id=Subscription_Id)
resource_list = resource_client.resources.list()
for item in resource_list:
    if(item.type == 'Microsoft.Compute/virtualMachines'):
        print(item.name)
Ecstasy
  • 1,866
  • 1
  • 9
  • 17
Hassan Turi
  • 334
  • 3
  • 14
  • Does this answer your question? [Azure python sdk get attributes from virtual machine](https://stackoverflow.com/questions/56451789/azure-python-sdk-get-attributes-from-virtual-machine) – Ecstasy Mar 29 '22 at 05:30
  • You can refer to [Python Azure sdk - Enabling Disk Encryption](https://stackoverflow.com/questions/59785672/python-azure-sdk-enabling-disk-encryption) and [Azure Virtual Machines Management Samples - Python](https://learn.microsoft.com/en-us/samples/azure-samples/virtual-machines-python-manage/azure-virtual-machines-management-samples---python/) – Ecstasy Mar 29 '22 at 05:30
  • @DeepDave-MT this is my output it does not have the key name `encryption` or `storage_profile` how can I get it `" 'name': 'Linux', 'type': 'Microsoft.Compute/virtualMachines', 'location': 'eastus', 'extended_location': None, 'tags': None, 'plan': None, 'properties': None, 'kind': None, 'managed_by': None, 'sku': None, 'identity': None, 'created_time': None, 'changed_time': None, 'provisioning_state': None"` – Hassan Turi Mar 29 '22 at 05:36
  • Could you please try to use `os_disk_name = virtual_machine.storage_profile.os_disk.name os_disk = compute_client.disks.get(GROUP_NAME, os_disk_name)` as mentioned in this [GitHub](https://github.com/azure-samples/virtual-machines-python-manage/tree/master/#retrieving-a-vms-os-disk) sample. – AjayKumarGhose Mar 29 '22 at 06:46

1 Answers1

0

To get the storage_profile you can use the below example of code with list all :

vm_list = compute_client.virtual_machines.list_all()

filtered = [vm for vm in vm_list if vm.name == "MY_VM_NAME"] 
vm = filtered[0] #First VM

print("\nstorageProfile")
print("  imageReference")
print("    publisher: ", vm.storage_profile.image_reference.publisher)
print("    offer: ", vm.storage_profile.image_reference.offer)
print("    sku: ", vm.storage_profile.image_reference.sku)
print("    version: ", vm.storage_profile.image_reference.version)
print("  osDisk")
print("    osType: ", vm.storage_profile.os_disk.os_type.value)
print("    name: ", vm.storage_profile.os_disk.name)
print("    createOption: ", vm.storage_profile.os_disk.create_option.value)
print("    caching: ", vm.storage_profile.os_disk.caching.value)

For Complete setup please refer this MICROSOFT DOCUMENTATION: Create and manage Windows VMs in Azure using Python

For more information please refer this SO THREAD: How could I list details of only 1 Azure Virtual Machines using Python?

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15