1

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)
4
  • Does this answer your question? Azure python sdk get attributes from virtual machine Commented Mar 29, 2022 at 5:30
  • You can refer to Python Azure sdk - Enabling Disk Encryption and Azure Virtual Machines Management Samples - Python Commented Mar 29, 2022 at 5: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" Commented Mar 29, 2022 at 5: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 sample. Commented Mar 29, 2022 at 6:46

1 Answer 1

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?

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.