python - 如何从python API azure sdk获取azure规模集实例的公共(public)IP?

标签 python azure azure-devops azure-functions azure-sdk

我已经为每个实例分配了公共(public)IP(没有负载均衡器),我尝试从python代码中获取它的公共(public)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)

但我没有在其中看到公共(public) IP 的属性。

最佳答案

我自己也不确定,所以我看了一下。事实证明,虚拟网络服务下有一个 API lists all public IP addresses of a scale set .

此代码应该适合您,它将列出规模集中使用的所有公共(public) 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

关于python - 如何从python API azure sdk获取azure规模集实例的公共(public)IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56922431/

相关文章:

.net - Azure构建管道: How to include additional project in artifact?

Python - 如果语句不能正常工作

azure - 文件监控文件名无效 : 'E:\sitesroot\0\CustomControls' . 失败常见原因包括

javascript - 如何使用 @azure/storage-blob sdk 附加 blob 文件? ( Node JS)

azure - 有没有办法在 host.json 文件中为 azure 函数设置 "extensionBundle"的批处理大小

azure - 从 yaml 管道中的变量组参数化 azureSubscription

azure-devops - Azure Pipelines:将变量作为参数传递给模板

python - 在 Python Cartopy 中向加拿大 map 添加省份

python - 查找平均值并显示 csv 中的最大数据(列表索引必须是整数或切片,而不是 str)#python

python - Jupyter笔记本-属性错误: 'module' object has no attribute 'F_OK'