How to Azure important attribute values for a given VM in Json format?

本秂侑毒 提交于 2020-07-23 06:20:52

问题


I am trying for getting instance,storage,disc,backup details from azure via python into output as json format , but i am not getting all values referred to documentation for describing instance but not able to find full fledged info.

I am looking at following items to be output as json format for a VM..

Instance Related: resource group name,status,Operating system,Size,Location ,Public Ip Address,VirtualNetwork/subnet Availability Zone,Private Ip,Static IP,VM Generation,

Size: Size,vcpu,RAM, Disk:OS Disk,Azure disk encryption,Data Disks

Volumes: Name,Size,Storage Account,Encryption

Volume Tags: its Key and value Backup:Backup pre-check,Last backup status,Backup policy:its name needed

Root volume : size and encryption

Swap Volume : size and encryption

can some one point to examples and correct syntax, basically looking like describe instance so that i can get all these values into json output...


回答1:


You can use the demo below, and try to use debug to fetch the information that you need:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient

SUBSCRIPTION_ID = 'xxx'
VM_NAME = 'xxx'

credentials = ServicePrincipalCredentials(
    client_id='xx',
    secret='xxx',
    tenant='xxx'
)

compute_client = ComputeManagementClient(
    credentials=credentials,
    subscription_id=SUBSCRIPTION_ID
)

vms = compute_client.virtual_machines.list_all()

myvm_resource_group=""

for vm in vms:
    if vm.name == VM_NAME:
        print(vm.id)

        #the vm.id is always in this format: 
        #'/subscriptions/your_subscription_id/resourceGroups/your_resource_group/providers/Microsoft.Compute/virtualMachines/your_vm_name'
        #so you can split it into list, and the resource_group_name's index is always 4 in this list.
        temp_id_list=vm.id.split('/')
        myvm_resource_group=temp_id_list[4]


#vm's resource group name
print("the vm test0's resource group is: " + myvm_resource_group)

# now you know the vm name and it's resourcegroup, you can use other methods,
# like compute_client.virtual_machines.get(resource_group_name, vm_name) to do any operations for this vm.


myvm = compute_client.virtual_machines.get(myvm_resource_group,VM_NAME, expand='instanceView')

#vm status
print("vm status: " + myvm.instance_view.statuses[1].display_status)

#vm Operating system
print("vm os name: " + myvm.instance_view.os_name)

#vm size
print("vm size: " + str(myvm.storage_profile.os_disk.disk_size_gb))

#vm location
print("vm location: " + myvm.location)

#network related properties

network_client = NetworkManagementClient(
    credentials=credentials,
    subscription_id=SUBSCRIPTION_ID
)

for interface in myvm.network_profile.network_interfaces:
    name=" ".join(interface.id.split('/')[-1:])
    sub="".join(interface.id.split('/')[4])

    ips=network_client.network_interfaces.get(sub,name).ip_configurations
    for ip in ips:
        print("vm private ip: " + ip.private_ip_address)

        #for public ip
        public_ip_name = "".join(ip.public_ip_address.id.split('/')[-1:])
        public_ip_resource_group = "".join(ip.public_ip_address.id.split('/')[4])
        public_ip = network_client.public_ip_addresses.get(public_ip_resource_group,public_ip_name)
        print("vm public ip: " + public_ip.ip_address)    

print("**completed**")

Here is the test result, and the debug info:



来源:https://stackoverflow.com/questions/62828586/how-to-azure-important-attribute-values-for-a-given-vm-in-json-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!