如何以 Json 格式为给定的 VM Azure 重要属性值?
How to Azure important attribute values for a given VM in Json format?
我正在尝试通过 python 从 azure 中获取实例、存储、磁盘、备份详细信息,并将其作为 json 格式输出,但我没有获取所有引用文档的值来描述实例,但是无法找到完整的信息。
我正在考虑将以下项目输出为 VM 的 json 格式..
实例相关:资源组名称,状态,操作系统,大小,位置,Public IP地址,VirtualNetwork/subnet可用区,私有IP,静态 IP,虚拟机生成,
大小:大小,vcpu,RAM,Disk:OS磁盘,Azure磁盘加密,数据磁盘
卷:名称、大小、存储帐户、加密
卷标签:其键和值Backup:Backup预检查,上次备份状态,备份policy:its需要名称
根卷: 大小和加密
交换卷:大小和加密
可以指出示例和正确的语法吗,基本上看起来像 describe instance 这样我就可以将所有这些值输入 json 输出...
您可以使用下面的演示,并尝试使用调试来获取您需要的信息:
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**")
这是测试结果和调试信息:
我正在尝试通过 python 从 azure 中获取实例、存储、磁盘、备份详细信息,并将其作为 json 格式输出,但我没有获取所有引用文档的值来描述实例,但是无法找到完整的信息。
我正在考虑将以下项目输出为 VM 的 json 格式..
实例相关:资源组名称,状态,操作系统,大小,位置,Public IP地址,VirtualNetwork/subnet可用区,私有IP,静态 IP,虚拟机生成,
大小:大小,vcpu,RAM,Disk:OS磁盘,Azure磁盘加密,数据磁盘
卷:名称、大小、存储帐户、加密
卷标签:其键和值Backup:Backup预检查,上次备份状态,备份policy:its需要名称
根卷: 大小和加密
交换卷:大小和加密
可以指出示例和正确的语法吗,基本上看起来像 describe instance 这样我就可以将所有这些值输入 json 输出...
您可以使用下面的演示,并尝试使用调试来获取您需要的信息:
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**")
这是测试结果和调试信息: