如何以Json格式为给定VM的Azure重要属性值? [英] How to Azure important attribute values for a given VM in Json format?

查看:40
本文介绍了如何以Json格式为给定VM的Azure重要属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python将azure的实例,存储,光盘,备份详细信息获取为json格式的输出,但是我没有获得描述实例的文档所引用的所有值,但无法找到完整的信息.

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.

我正在寻找以下项目,以VM的json格式输出.

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

实例相关:资源组名称,状态,操作系统,大小,位置,公共IP地址,虚拟网络/子网可用区,专用IP,静态IP,VM生成

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

大小:大小,vcpu,RAM,磁盘:OS磁盘,Azure磁盘加密,数据磁盘

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

:名称,大小,存储帐户,加密

Volumes: Name,Size,Storage Account,Encryption

卷标:其键和值Backup:备份预检查,最后备份状态,备份策略:需要其名称

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

根卷:大小和加密

交换量:大小和加密

可以指出示例和正确的语法,基本上看起来像是描述实例,这样我就可以将所有这些值都输入到json输出中...

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

推荐答案

您可以使用下面的演示,并尝试使用debug来获取所需的信息:

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:

这篇关于如何以Json格式为给定VM的Azure重要属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆