gpt4 book ai didi

azure - terraform:仅将公共(public)IP添加到一个azure VM

转载 作者:行者123 更新时间:2023-12-02 05:51:34 25 4
gpt4 key购买 nike

我正在 azurerm_virtual_machine 中通过计数创建 4 个虚拟机,但我只想创建一个公共(public) IP 并将其与第一个虚拟机关联?这可能吗?如果可以的话怎么办?

下面是我的模板文件

resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
}
}

resource "azurerm_public_ip" "public_ip" {
name = ...
location = ...
resource_group_name = ...
}

resource "azurerm_virtual_machine" "vms" {
count = 4
network_interface_ids = [element(azurerm_network_interface.nics.*.id, count.index)]
}

我已经解决了以下问题,但它们创建了多个公共(public) IP 并将它们添加到所有虚拟机。

multiple-vms-with-public-ip

set-dynamic-ip

attach-public-ip

最佳答案

公共(public) IP 是使用 azurerm_public_ip 创建的:

resource "azurerm_public_ip" "public_ip" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}

azurerm_network_interface 中获得地址后,您可以使用 Conditional Expressions 执行以下操作:

resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...

ip_configuration {

subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...

public_ip_address_id = count.index == 1 ? azurerm_public_ip.public_ip.id : null
}
}

关于azure - terraform:仅将公共(public)IP添加到一个azure VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65935259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com