【问题标题】:Is there a python class in Azure SDK to reserve Public IP address of an instance?Azure SDK 中是否有 python 类来保留实例的公共 IP 地址?
【发布时间】:2019-05-02 00:36:12
【问题描述】:

我试图在关闭或取消分配 Azure VM 之前保留它的公共 IP 地址,但我无法在 Azure SDK 中为此找到任何 python 类。当我在 UI 中关闭我的 VM 时,它会询问我是否要保留公共 IP 地址。我想要具有相同功能的类。我该如何解决这个问题?

我尝试将公共 IP 保存到变量并在我的 VM 关闭后将其传递给 NIC,但结果它不接受我提供的 IP。

network_client = NetworkManagementClient(credentials=setup['values'], subscription_id=test_bed.subscription_id)

parapip = network_client.models().PublicIPAddress(location='West US', public_ip_allocation_method='Static', ip_address='104.42.218.83')

network_client.public_ip_addresses.create_or_update(resource_group_name=setup['resource_group_name'], public_ip_address_name='mgmt2-pip-jp07zf', parameters=parapip)

【问题讨论】:

    标签: python azure sdk


    【解决方案1】:

    事实上,如果您有一个与该 Azure VM 关联的动态分配公共 IP 地址,当您在 Azure 门户中停止 VM 时,UI 会询问您是否要保留公共 IP 地址。如果您在停止 VM 之前有一个静态分配的公共 IP 地址,则 UI 不会询问您是否要保留公共 IP 地址。

    为此,如果您已经有一个静态公共 IP,那么即使您停止 VM,该 IP 地址也不会丢失。您可以更改代码以动态设置 IP 地址。例如,要为虚拟机创建一个公共 IP 地址,在 .py 文件中的变量之后添加此函数:

    def create_public_ip_address(network_client): public_ip_addess_params = { 'location': LOCATION, 'public_ip_allocation_method': 'Dynamic' } creation_result = network_client.public_ip_addresses.create_or_update( GROUP_NAME, 'myIPAddress', public_ip_addess_params ) return creation_result.result()

    如果你想释放虚拟机,你可以使用这个代码,

    def stop_vm(compute_client):
        compute_client.virtual_machines.deallocate(GROUP_NAME, VM_NAME)
    

    更多详情来自Create and manage Windows VMs in Azure using Python。

    【讨论】:

    • 是的,我想知道是否有一个类可以像 UI 一样保留 IP 地址。像 - network_client.public_ip_addresses.reserve() - 像这样?
    • 我认为没有这样的课程。 UI 中的 reserve() 操作实际上是更改公共 IP 分配静态。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多