我尝试使用null_resource
使用triggers
枚举地图变量,并在另一个资源中使用此枚举的结果。
这有效:
resource "null_resource" "dummy" {
count = "${length(var.file_map)}"
triggers {
filename = "${element(keys(var.file_map), count.index)}"
content = "${var.file_map[element(keys(var.file_map), count.index)]}"
}
}
variable "file_map" {
type = "map"
default = {
"foo.txt" = "foo"
"bar.txt" = "bar"
}
}
输出:
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
+ null_resource.dummy[0]
id: <computed>
triggers.%: "2"
triggers.content: "bar"
triggers.filename: "bar.txt"
+ null_resource.dummy[1]
id: <computed>
triggers.%: "2"
triggers.content: "foo"
triggers.filename: "foo.txt"
Plan: 2 to add, 0 to change, 0 to destroy.
但是当我尝试在另一个资源中使用枚举结果时,它失败了:
resource "local_file" "some_files" {
content = "${null_resource.dummy.triggers.content}"
filename = "${null_resource.dummy.triggers.filename}"
}
resource "null_resource" "dummy" {
count = "${length(var.file_map)}"
triggers {
filename = "${element(keys(var.file_map), count.index)}"
content = "${var.file_map[element(keys(var.file_map), count.index)]}"
}
}
variable "file_map" {
type = "map"
default = {
"foo.txt" = "foo"
"bar.txt" = "bar"
}
}
输出:
Error running plan: 1 error(s) occurred:
* local_file.some_files: 1 error(s) occurred:
* local_file.some_files: Resource 'null_resource.dummy' not found for variable
'null_resource.dummy.triggers.content'
有没有办法让它发挥作用?
答案 0 :(得分:1)
资源YouTube('video_url').streams.first().download('save_path')
中element
的类似用法,您需要在null_resource
中执行相同操作。
local_file
运行resource "local_file" "some_files" {
count = "${length(var.file_map)}"
content = "${element(null_resource.dummy.*.triggers.content, count.index)}"
filename = "${element(null_resource.dummy.*.triggers.filename, count.index)}"
}
resource "null_resource" "dummy" {
count = "${length(var.file_map)}"
triggers {
filename = "${element(keys(var.file_map), count.index)}"
content = "${var.file_map[element(keys(var.file_map), count.index)]}"
}
}
variable "file_map" {
type = "map"
default = {
"foo.txt" = "foo"
"bar.txt" = "bar"
}
}
后,会生成两个文件
terraform apply
如果您想了解更多信息,$ cat bar.txt
bar
$ cat foo.txt
foo
将为您提供有关其工作原理的详细信息。