Terraform - 从值列表创建对象列表
Terraform - Create a List of Objects from List of Values
我 运行 在 Terraform v 0.11.8 上遇到了一个奇怪的问题。我们正在尝试关闭 ACR 的端口,并使其仅在网络内可用,并且还允许应用程序服务访问它。
terraform IP_restriction 规则文档显示类似这样的内容。
network_rule_set {
default_action = "Deny"
**ip_rule = [{
action = "Allow"
ip_range = "x.x.x.x"
},
{
action = "Allow"
ip_range = "y.y.y.y"
}...]**
}
我的 variable/local
中有 IP 列表
variable "myIps" {
type="list"
default="[x.x.x.x, y.y.y.y, z.z.z.z, ....]"
}
如何将元素列表 [x.x.x.x] 转换为对象列表
[{动作 = "Allow" ip_range = "x.x.x.x"}]。第一个 属性 动作 = "Allow" 总是静态的。我必须将 IP 从我的变量传递到对象 属性。
我试过像
这样的正则表达式模式
variable "test2" {
type="string"
default = "{action=\"Allow\", ip_range=\"%s\"}"
}
但是这个 returns 字符串不是对象列表。
谢谢!
您可以使用 for
循环来迭代 ip_rule
条目。
这是我这边使用 Terraform 的工作示例 v0.12.9
+ provider.azurerm v1.36.1
.
resource "azurerm_resource_group" "test" {
name = "example-test"
location = "East US"
}
variable "ips" {
type= "list"
default= ["8.8.8.8", "1.1.1.1","2.2.2.2"]
}
resource "azurerm_container_registry" "acr" {
name = "mytestacr123"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
admin_enabled = false
sku = "Premium"
# georeplication_locations = ["East US"]
network_rule_set {
default_action = "Deny"
# ip_rule block
ip_rule = [
for ip in var.ips: {
action = "Allow"
ip_range = ip
}
]
}
}
结果:
我 运行 在 Terraform v 0.11.8 上遇到了一个奇怪的问题。我们正在尝试关闭 ACR 的端口,并使其仅在网络内可用,并且还允许应用程序服务访问它。
terraform IP_restriction 规则文档显示类似这样的内容。
network_rule_set {
default_action = "Deny"
**ip_rule = [{
action = "Allow"
ip_range = "x.x.x.x"
},
{
action = "Allow"
ip_range = "y.y.y.y"
}...]**
}
我的 variable/local
中有 IP 列表variable "myIps" {
type="list"
default="[x.x.x.x, y.y.y.y, z.z.z.z, ....]"
}
如何将元素列表 [x.x.x.x] 转换为对象列表 [{动作 = "Allow" ip_range = "x.x.x.x"}]。第一个 属性 动作 = "Allow" 总是静态的。我必须将 IP 从我的变量传递到对象 属性。
我试过像
这样的正则表达式模式variable "test2" {
type="string"
default = "{action=\"Allow\", ip_range=\"%s\"}"
}
但是这个 returns 字符串不是对象列表。
谢谢!
您可以使用 for
循环来迭代 ip_rule
条目。
这是我这边使用 Terraform 的工作示例 v0.12.9
+ provider.azurerm v1.36.1
.
resource "azurerm_resource_group" "test" {
name = "example-test"
location = "East US"
}
variable "ips" {
type= "list"
default= ["8.8.8.8", "1.1.1.1","2.2.2.2"]
}
resource "azurerm_container_registry" "acr" {
name = "mytestacr123"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
admin_enabled = false
sku = "Premium"
# georeplication_locations = ["East US"]
network_rule_set {
default_action = "Deny"
# ip_rule block
ip_rule = [
for ip in var.ips: {
action = "Allow"
ip_range = ip
}
]
}
}
结果: