我已经为每个实例分配了公共IP(没有负载均衡器),我试图从python代码中获取它的公共IP,但是没有运气,到目前为止我一直在尝试:
from azure.mgmt.compute import ComputeManagementClient
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.network import NetworkManagementClient
credentials = ServicePrincipalCredentials(client_id=ID, secret=SECRET_KEY, tenant=TENANT_ID)
for net in NetworkManagementClient(credentials, SUBSCRIPTION_ID):
print net
IP不在这里。 我也尝试通过从此返回的比例尺设置对象:
vmss = ComputeManagementClient(credentials, SUBSCRIPTION_ID).virtual_machine_scale_set_vms.list(resource_group_name=resource_group,
virtual_machine_scale_set_name=scale_set_name)
但是我看不到公共IP的属性。
答案 0 :(得分:1)
我自己不确定这一点,所以我看看了。原来在虚拟网络服务下有一个lists all public IP addresses of a scale set的API。
此代码将为您工作,它将在秤组内列出所有正在使用的公共IP地址。
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
# Your Azure Subscription ID
subscription_id = 'xxxx-xxxx-xxxx'
compute_client = ComputeManagementClient(credentials, subscription_id)
network_client = NetworkManagementClient(credentials, subscription_id)
rg = 'testscaleset-rg'
scaleset_name = 'testscaleset'
for i, vm in enumerate(compute_client.virtual_machine_scale_set_vms.list(resource_group_name=rg, virtual_machine_scale_set_name=scaleset_name)):
nic_name = (vm.network_profile.network_interfaces[0].id).split("/")[-1]
ip_config_name = vm.network_profile_configuration \
.network_interface_configurations[0]\
.ip_configurations[0]\
.name
ip_address_name = vm.network_profile_configuration \
.network_interface_configurations[0]\
.ip_configurations[0]\
.public_ip_address_configuration\
.name
print(vm.name, (network_client.public_ip_addresses.get_virtual_machine_scale_set_public_ip_address( \
resource_group_name=rg, \
virtual_machine_scale_set_name=scaleset_name,\
virtualmachine_index=i, \
network_interface_name=nic_name, \
ip_configuration_name=ip_config_name, \
public_ip_address_name=ip_address_name)).ip_address)
应该返回
testscaleset_0 40.68.133.234