How To Azure Important Attribute Values For A Given Vm In Json Format?
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 descri
Solution 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 nameprint("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 statusprint("vm status: " + myvm.instance_view.statuses[1].display_status)
#vm Operating systemprint("vm os name: " + myvm.instance_view.os_name)
#vm sizeprint("vm size: " + str(myvm.storage_profile.os_disk.disk_size_gb))
#vm locationprint("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:
Post a Comment for "How To Azure Important Attribute Values For A Given Vm In Json Format?"