无法在Azure中获取使用Python SDK设置的虚拟机规模的公共IP地址

时间:2020-10-17 08:29:26

标签: azure azure-sdk-python

我有一个虚拟机规模集,并且连接了两个VM,我想使用Python SDK来获取其公共IP地址。

我正在使用此脚本来获取IP配置。

def get_vmss_vm_ips():

    # List all network interfaces of the VMSS instance
        vmss_nics = network_client.network_interfaces.list_virtual_machine_scale_set_network_interfaces(
            rg, vmscalesetname)

        niclist = [nic.serialize() for nic in vmss_nics]

        print "IP addresses in the given VM Scale Set:"

        for nic in niclist:
            ipconf = nic['properties']['ipConfigurations']
            print ipconf

这是我的输出:

{'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Compute/virtualMachineScaleSets/EchoServer/virtualMachines/2/networkInterfaces/test-vnet-nic01/ipConfigurations/test-vnet-nic01-defaultIpConfiguration', 'properties': {'subnet': {'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Network/virtualNetworks/test-vnet/subnets/default'}, 'privateIPAddressVersion': u'IPv4', 'publicIPAddress': {'id': u'/subscriptions/sub_id/resourceGroups/test/providers/Microsoft.Compute/virtualMachineScaleSets/EchoServer/virtualMachines/2/networkInterfaces/test-vnet-nic01/ipConfigurations/test-vnet-nic01-defaultIpConfiguration/publicIPAddresses/publicIp-test-vnet-nic01'}, 'privateIPAllocationMethod': u'Dynamic', 'primary': True, 'privateIPAddress': u'10.0.0.9'}, 'name': u'test-vnet-nic01-defaultIpConfiguration'}]

我可以看到privateIPAddress,但看不到publicIPAddress。如何获取公共IP地址?

1 个答案:

答案 0 :(得分:0)

如果要使用Azure规模设置虚拟机的公用IP地址,请参考以下脚本

  1. 创建服务主体并将for row in range(2, sheet.max_row + 1): cell = sheet.cell(row, 3) new_price = cell.value new_price = float(new_price.replace("$","")) * 0.9 new_price_cell = sheet.cell(row, 4) new_price_cell.value = new_price 角色分配给服务主体

    Contributor
  2. 代码

    az login
    az ad sp create-for-rbac -n "MyApp" --role contributor \
        --scopes /subscriptions/{SubID} --sdk-auth true
    

    Enter image description here