[英]Trouble referring type list(object) variable from module directory using for_each into main dir
[英]Accessing for_each variables of type list(object) from a module
我创建了一个模块,它为 VM 执行端口转发,并且在类型列表(对象)中有一个变量,我无法引用它。
proxmox-instance(下面包含树)中有一个 main.tf 文件,其中包含我们的变量。 它有一个 for_each 循环,我正在尝试在不同的目录上使用 ./proxmox-instance/main.tf 的变量循环内的对象 - the。 目录里面./main.tf.
.
├── main.tf
├── output.tf
└── proxmox-instance
├── main.tf
├── output.tf
├── providers.tf
└── variables.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 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.host_port
forwarded_ports.guest_port
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
forwarded_ports.guest_port = 22
}
我试过使用主机/来宾端口的语法,但不能完全理解它,所以一直收到错误。
Output 1/2
forwarded_ports.host_port = 2222
forwarded_ports.guest_port = 22
Error: Argument or block definition required
│
│ on main.tf line 17, in module "instance":
│ 17: forwarded_ports.guest_port = 22
│
│ An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
Output 2/2
forwarded_ports = [{
host_port = 2222
guest_port = 22
}]
│ Error: Missing required argument
│
│ on main.tf line 5, in module "instance":
│ 5: module "instance" {
│
│ The argument "host_port" is required, but no definition was found.
│ Error: Missing required argument
│
│ on main.tf line 5, in module "instance":
│ 5: module "instance" {
│
│ The argument "guest_port" is required, but no definition was found.
在我看来,至少有两件事是错误的[可能更多是因为缺少我无法提交的代码]
1:在你的./proxmox-instance/main.tf
中, for_each = [for k in forwarded_ports: k]
需要引用 terraform 中定义的变量。
正确参考:
resource "iptables_nat" "dnat-front" {
# Creates an entry for each template specified
for_each = [for k in var.forwarded_ports : k] ## change is ** var.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
}
}
[for k in var.forwarded_ports: k]
会给你下面的元组,你在你的模块接口调用中工作。
[
{
"guest_port" = 22
"host_port" = 2222
},
]
您可以在 terraform 控制台中验证这一点。 有关 Terraform 控制台的更多详细信息,请参见 此处
2:你的模块接口调用也是错误的,报错信息An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
抱怨forwarded_ports.host_port = 2222
和forwarded_ports.guest_port = 22
。
由于此处粘贴的代码片段不完整,因此似乎缺少一些其他变量和资源。 请尝试使用类似
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"
## module will refer to the variables and then pass them in the correct type.
forwarded_ports = [
{
host_port = 2222
guest_port = 22
},
{
host_port = another_value
guest_port = another_value
},
]
}
我希望这有帮助。
在变量中使用 object 时,您必须以与在模块中使用默认值大致相同的方式定义它。
你得到的第一个错误解释得最好:
Error: Argument or block definition required
│
│ on main.tf line 17, in module "instance":
│ 17: forwarded_ports.guest_port = 22
│
│ An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.
要在模块中引用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
}]
}
模块内的variables
是模块块中的输入。 任何不包含默认值的变量都是必需的,而包含默认值的变量是可选的。
来自Output 2/2的第二个错误:
│ Error: Missing required argument
│
│ on main.tf line 5, in module "instance":
│ 5: module "instance" {
│
│ The argument "host_port" is required, but no definition was found.
表示还有其他名为host_port
和guest_port
的变量与forwarded_ports
变量无关,显然是必需的。 你的代码没有显示这个,但我怀疑你会在你的模块variables.tf
文件中找到这两个。 您看到此错误的可能原因是因为您在该示例中正确定义了forwarded_ports
,但遗漏了其他不相关的 arguments,其名称让您感到困惑。
要了解有关模块输入的更多信息,请参阅Terraform 关于输入变量的文档。
./proxmox-instance/main.tf
resource "iptables_nat" "dnat-front" {
# Creates an entry for each template specified
for_each = [for k in forwarded_ports : k]
...
这应该是var.forwarded_ports
并且应该看起来像这样:
resource "iptables_nat" "dnat-front" {
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
nat_ip = "${proxmox_vm_qemu.vm.default_ipv4_address}:${each.value.guest_port}" # VM Internal IP here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.