gpt4 book ai didi

azure - 通过 terraform 有条件地添加到资源属性

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

我正在尝试通过 terraform 创建服务结构集群,我希望动态添加 1 或 2 个节点类型参数。

我的集群定义如下:

resource "azurerm_service_fabric_cluster" "example" {
name = "example-servicefabric"
resource_group_name = "${var.cluster_name}-group"
location = var.location
reliability_level = "Bronze"
upgrade_mode = "Manual"
cluster_code_version = "7.1.456.959"
vm_image = "Windows"
management_endpoint = "https://example:80"

node_type{
name = "first"
instance_count = 3
is_primary = true
client_endpoint_port = 2020
http_endpoint_port = 80
}

node_type{
name = "second"
instance_count = 3
is_primary = true
client_endpoint_port = 2020
http_endpoint_port = 80
}
}

我想要的是,当变量为 false 时仅部署“第一个”node_type,并在变量为 true 时部署“第一个”和“第二个”。

通常,如果我正在部署资源,我会使用

  count                = var.node_type_count > 1 ? 1 : 0

但是这是无法完成的,因为节点类型本身不是资源,它们只是属性。我如何有条件地添加到此?

最佳答案

您可以使用 dynamic blocks 。基本上,first 总是被创建,而 second 是可选的:

resource "azurerm_service_fabric_cluster" "example" {
name = "example-servicefabric"
resource_group_name = "${var.cluster_name}-group"
location = var.location
reliability_level = "Bronze"
upgrade_mode = "Manual"
cluster_code_version = "7.1.456.959"
vm_image = "Windows"
management_endpoint = "https://example:80"


node_type {
name = "first"
instance_count = 3
is_primary = true
client_endpoint_port = 2020
http_endpoint_port = 80
}

dynamic "node_type" {

for_each = var.second_node == true ? [1] : []

content {
name = "second"
instance_count = 3
is_primary = true
client_endpoint_port = 2020
http_endpoint_port = 80
}
}

}

关于azure - 通过 terraform 有条件地添加到资源属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67329334/

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