当我在资源定义中使用count或for_each时,资源是并行创建的。是否有一种解决办法使之连续进行?首先使用count.index=0创建资源,然后创建count.index=1,然后创建count.index=2 e.t.c。
一点背景..。我在用terraform做初始的Hyperledger织物安装。对于某些任务,我需要依次对区块链网络进行配置更新(比如批准参与组织的链码)。否则我得到(MVCC_READ_CONFLICT)。如果我把这个逻辑完全外包给某个bash脚本,这当然是可以实现的,但也许.
发布于 2020-11-09 09:58:53
因此,要使事情顺利进行,就需要在其他地方储存一种状态。最简单的事情是使用一个文件:
resource "null_resource" "set_initial_state" {
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "echo \"0\" > current_state.txt"
}
}第二个资源在开始时实现等待循环,最后实现状态更改:
resource "null_resource" "sequential_resources" {
count = var.x
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "while [[ $(cat current_state.txt) != \"${count.index}\" ]]; do echo \"${count.index} is waiting...\";sleep 5;done"
}
# Here you pack your sequential logic, e.g. upload of files to a service,
# that can handle only one file at once.
provisioner "file" {
connection {
type = "ssh"
host = var.ip_address
private_key = file(var.config.private_key)
user = var.config.admin_username
script_path = var.provisioner_script_path
}
source = "${var.local_folder}/file_${count.index}.txt"
destination = "/opt/data/file_${count.index}.txt"
}
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "echo \"${count.index+1}\" > current_state.txt"
}
depends_on = [null_resource.set_initial_state]
}在本例中,首先上载file_0.txt,然后是file_1.txt,然后是file_2.txt e.t.c。直到file_x.txt
发布于 2020-09-04 17:08:48
在我看来,您正在寻找一种方法来创建对各种资源的依赖关系,因此它们是以您描述的顺序方式部署的。在资源块中实现这一目标的一种方法是使用Terraforms参数depends_on (下面的链接)。这个元参数的作用和它听起来完全一样。它允许您在资源(资源A)中定义同一模块(资源B)中的另一个资源的名称,以便在执行之前完成。换句话说,资源A在资源B完成之前不会启动。当与循环(如for_each和count )一起使用时,我不确定这种行为
Note:Terraform建议将此作为最后手段。查看他们的文档以获得更多细节。
https://www.terraform.io/docs/configuration/resources.html#depends_on-explicit-resource-dependencies
https://stackoverflow.com/questions/63744524
复制相似问题