当我在资源定义中使用 count 或 for_each 时,资源是并行创建的。有没有一种解决方法可以使其顺序化?首先创建 count.index=0 的资源,然后 count.index=1,然后 count.index=2 等等
一些背景知识...我正在使用 terraform 进行初始 Hyperledger Fabric 设置。对于某些任务,我需要按顺序进行区块链网络的配置更新(例如为参与组织批准链代码)。否则我会得到(MVCC_READ_CONFLICT)。如果我将此逻辑完全外包给某些 bash 脚本,这当然可以实现,但也许......
因此,为了使事情正常进行,我们需要在其他地方存储一种状态。最简单的是使用文件:
resource "null_resource" "set_initial_state" {
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = "echo \"0\" > current_state.txt"
}
}
Run Code Online (Sandbox Code Playgroud)
第二个资源在开始时实现等待循环,在结束时实现状态更改:
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]
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,首先上传 file_0.txt,然后上传 file_1.txt,然后上传 file_2.txt,依此类推,直到 file_x.txt
| 归档时间: |
|
| 查看次数: |
10705 次 |
| 最近记录: |