gpt4 book ai didi

linux - 引用在 terraform 循环中创建的特定 public_ip

转载 作者:太空宇宙 更新时间:2023-11-04 05:46:09 25 4
gpt4 key购买 nike

我无法正确地将使用 terraform 创建的第一个 azure 公共(public) IP 引用到使用相同 terraform 脚本创建的网络安全组中。

我尝试了各种方法以使该行正常:

source_address_prefix      = "${(count.index == 0 ? var.incoming_ip : azurerm_public_ip.test.0.)}"

我尝试过:

azurerm_public_ip.test.0.name
azurerm_public_ip.test.0.ip_adress
azurerm_public_ip.test[0]

但是我在部署时遇到错误,它似乎将 null 或字符串传递给脚本,而不是使用 count.index 为 0 创建的第一个实例的 public_ip。

resource "azurerm_public_ip" "test" {
name = "publicIP-${count.index}"
location = "${var.regions[count.index]}"
resource_group_name = "${element(azurerm_resource_group.test.*.name, count.index)}"
allocation_method = "Static"
count = "${length(var.regions)}"
}

resource "azurerm_network_security_group" "test" {
name = "${var.nameid[count.index]}"
count = "${length(var.regions)}"
location = "${var.regions[count.index]}"
resource_group_name = "${element(azurerm_resource_group.test.*.name, count.index)}"

security_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "${var.incoming_ip}"
destination_address_prefix = "*"
}

security_rule {
name = "curl"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "${(count.index == 0 ? 8888 : 7777)}"
source_address_prefix = "${(count.index == 0 ? var.incoming_ip : azurerm_public_ip.test.0.)}"
destination_address_prefix = "*"
}
}

我期待这一行:

source_address_prefix      = "${(count.index == 0 ? var.incoming_ip : azurerm_public_ip.test.0.name)}"

以这种方式行事:

如果 count.index == 0,则为 var.incoming_ip 的值,否则为在索引 0 处创建的 Azure 公共(public) IP 的值。

最佳答案

这个语法成功了:

source_address_prefix      = "${(count.index == 0 ? var.incoming_ip : element(azurerm_public_ip.test.*.ip_address, 0))}"

关于linux - 引用在 terraform 循环中创建的特定 public_ip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58227881/

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