检索供应者命令的值?

时间:2018-09-27 14:44:18

标签: terraform

这与“ Capture Terraform provisioner output?”不同。我有一个资源(在这种情况下为null_resource),它带有一个count和一个local-exec的预配器,该预配器具有一些复杂的插值参数:

resource "null_resource" "complex-provisioning" {
  count = "${var.count}"

  triggers {
    server_triggers = "${null_resource.api-setup.*.id[count.index]}"
    db_triggers   = "${var.db_id}"
  }

  provisioner "local-exec" {
    command = <<EOF
${var.init_command}
do-lots-of-stuff --target=${aws_instance.api.*.private_ip[count.index]} --bastion=${aws_instance.bastion.public_ip} --db=${var.db_name}
EOF
  }
}

我希望能够显示配置程序作为输出所做的操作(这不是有效的Terraform,只是我想要的模型):

output "provisioner_commands" {
  value = {
    api_commands = "${null_resource.complex-provisioning.*.provisioner.0.command}"
  }
}

我的目标是获得类似的输出

provisioner_commands = {
  api_commands = [
    "do-lots-of-stuff --target=10.0.0.1 --bastion=77.2.4.34 --db=mydb.local",
    "do-lots-of-stuff --target=10.0.0.2 --bastion=77.2.4.34 --db=mydb.local",
    "do-lots-of-stuff --target=10.0.0.3 --bastion=77.2.4.34 --db=mydb.local",
  ]
}

我可以读取预配器配置并像这样输出吗?如果没有,是否有其他方法可以得到我想要的东西? (如果不需要在一系列资源上运行,则可以在local变量中定义命令,并在配置程序和输出中引用该命令。)

1 个答案:

答案 0 :(得分:1)

您无法从local-exec供应器块中获取插值命令,但是如果将相同的插值放入触发器中,则可以在输出中使用0.12.x中的for表达式检索该插值命令

resource "null_resource" "complex-provisioning" {
  count = 2

  triggers = {
    command = "echo ${count.index}"
  }

  provisioner "local-exec" {
    command = self.triggers.command
  }
}

output "data" {
  value = [
    for trigger in null_resource.complex-provisioning.*.triggers:
    trigger.command
  ]
}
$ terraform apply
null_resource.complex-provisioning[0]: Refreshing state... [id=9105930607760919878]
null_resource.complex-provisioning[1]: Refreshing state... [id=405391095459979423]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

data = [
  "echo 0",
  "echo 1",
]