下面是我使用的代码示例:
main.tf
module "bar" {
...
nets = "${module.foo.nets_ids}"
}
variables.tf
variable "nets" {
description = "nets desc."
type = "list"
}
module bar's main.tf
data "template_file" "k8s_yaml" {
template = "${file("${path.module}/car.tpl")}"
vars = {
...
nets = "${var.nets}"
...
}
}
resource "null_resource" "k8s" {
provisioner "local-exec" {
command = "echo '${data.template_file.k8s_yaml.rendered}' | ... "
}
}
module bar's variables.tf
variable "nets" {
description = "nets desc."
type = "list"
}
在我尝试制定地形图后,我收到:
* module.bar.data.template_file.k8s_yaml: vars: 1 error(s) decoding:
* '[nets]' expected type 'string', got unconvertible type '[]interface {}'
我发现这真的很奇怪,因为我在所有的vars定义中都指定了type: list
,为什么它认为它需要一个字符串?
这是我的tpl文件的一个片段:
netsIDs:
- ${element(var.nets, 0)}
- ${element(var.nets, 1)}
- ${element(var.nets, 2)}
显然netsIDs: ${nets}
和nets = "${jsonencode(var.nets)}"
解决了这个问题。