繁体   English   中英

无法使用 for_each 将模块目录中的类型列表(对象)变量引用到主目录中

[英]Trouble referring type list(object) variable from module directory using for_each into main dir

我在模块目录上创建了一个类型为 list(object) 的变量。 我的目标是使用 for_each 循环在不同目录下调用此变量。 (下面附上树)

.
├── main.tf
├── output.tf
└── proxmox-instance
    ├── main.tf
    ├── output.tf
    ├── providers.tf
    └── variables.tf

variable.tf 文件看起来像这样

variable "forwarded_ports" {
  description = "Your Template name - Will be cloned from this template"
  type = list(object({
    host_port  = number
    guest_port = number
  }))
  default = [{
    "host_port"  = 2222,
    "guest_port" = 22
  }]
}

虽然 ./proxmox-instance/main.tf 看起来像这样

resource "iptables_nat" "dnat-front" {
  # Creates an entry for each template specified
  for_each = [for k in var.forwarded_ports : k]

  name           = "iptable-${var.vm_name}-${each.value.host_port}-to-${each.value.guest_port}"
  on_cidr_blocks = ["195.201.172.34"] # Proxmox network device IP here

  dnat {
    iface    = "enp7s0"
    protocol = "tcp"
    to_port  = each.value.host_port                                                  # Custom Port for ingress connections
    nat_ip   = "${proxmox_vm_qemu.vm.default_ipv4_address}:${each.value.guest_port}" # VM Internal IP here
  }
}

尝试在下面的forwarded_ports部分引用代码时出现问题

module "instance" {
  source = "./proxmox-instance"

  vm_name       = "Terraform"
  template_name = "ubuntu-20.04"

  # cloud-init settings
  ipconfig0           = "ip=dhcp"
  cloud_init_user     = "ubuntu"
  cloud_init_password = "password"

  forwarded_ports = [
    {
      host_port  = 2222
      guest_port = 22
    },
    {
      host_port  = 5555
      guest_port = 22
    },
  ]
}

我试过使用语法 abit 并得到了不同的结果

Output 1/2

for_each = [for k in var.forwarded_ports : k]


│ Error: Invalid for_each argument
│ 
│   on proxmox-instance\main.tf line 37, in resource "iptables_nat" "dnat-front":
│   37:   for_each = [for k in var.forwarded_ports : k]
│     ├────────────────
│     │ var.forwarded_ports is list of object with 2 elements
│ 
│ The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type tuple.

Output 2/2

for_each = toset([for k in var.forwarded_ports : k])


│ Error: Invalid for_each set argument
│ 
│   on proxmox-instance\main.tf line 37, in resource "iptables_nat" "dnat-front":
│   37:   for_each = toset([for k in var.forwarded_ports : k])
│     ├────────────────
│     │ var.forwarded_ports is list of object with 2 elements
│
│ The given "for_each" argument value is unsuitable: "for_each" supports maps and sets of strings, but you have provided a set containing type object.

您正在提供一个类型为list(objects)的变量,并且您正在尝试将列表与for_each一起使用(错误 output 表明这是不可能的),而它只能与地图或集合一起使用。 您可以尝试以下操作:

resource "iptables_nat" "dnat-front" {
  # Creates an entry for each template specified
  for_each = { for i in var.forwarded_ports : i.host_port => {
    host_port  = i.host_port
    guest_port = i.guest_port
  } }

  name           = "iptable-${var.vm_name}-${each.value.host_port}-to-${each.value.guest_port}"
  on_cidr_blocks = ["195.201.172.34"] # Proxmox network device IP here

  dnat {
    iface    = "enp7s0"
    protocol = "tcp"
    to_port  = each.value.host_port                                                  # Custom Port for ingress connections
    nat_ip   = "${proxmox_vm_qemu.vm.default_ipv4_address}:${each.value.guest_port}" # VM Internal IP here
  }
}

或者,您可以定义一个locals块并在那里创建您想要的 map:

locals {
  fowarded_ports = { for i in local.forwarded_ports : i.host_port => {
    host_port  = i.host_port
    guest_port = i.guest_port
  } }
}

然后在资源块中:

resource "iptables_nat" "dnat-front" {
  # Creates an entry for each template specified
  for_each = local.forwarded_ports

  name           = "iptable-${var.vm_name}-${each.value.host_port}-to-${each.value.guest_port}"
  on_cidr_blocks = ["195.201.172.34"] # Proxmox network device IP here

  dnat {
    iface    = "enp7s0"
    protocol = "tcp"
    to_port  = each.value.host_port                                                  # Custom Port for ingress connections
    nat_ip   = "${proxmox_vm_qemu.vm.default_ipv4_address}:${each.value.guest_port}" # VM Internal IP here
  }
}

forwarded_ports是一个列表。 for_each适用于地图。 您可以轻松地将列表转换为 map,如下所示:

 for_each = { for i,v in var.forwarded_ports : i => v}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM