仅返回目标模块的输出

时间:2017-12-22 00:34:15

标签: terraform

我有一个调用一堆模块的tf文件:

variable "myvar" {}

module "mod1" {
  arg = "${var.myvar}"
  paramblah = "sldkfjsldkfj"
  source = "...my git repo..."
}

module "mod2" {
  arg = "${var.myvar}"
  paramblah = "sdifjsdfssldkfjsldkfj"
  source = "...my git repo..."
}

module "mod3" {
  arg = "${var.myvar}"
  paramblah = "qiejiqjd"
  source = "...my git repo..."
}

output "myoutput" {
  value = ["${module.mod1.modsOutput}",
    "${module.mod2.modsOutput}",
    "${module.mod3.modsOutput}"]
}

有时我会在tf文件中定位一个模块:

terraform apply -var myvar=sdfsdfsdf -target='module.mod2'

有没有办法让terraform output仅返回我定位的最后一个模块的输出?现在,如果我运行terraform output myoutput,它将返回我在列表中定义的所有模块的输出。

1 个答案:

答案 0 :(得分:1)

我只需要在个别输出中将它们分解出来:

variable "myvar" {}

module "mod1" {
  arg = "${var.myvar}"
  paramblah = "sldkfjsldkfj"
  source = "...my git repo..."
}

module "mod2" {
  arg = "${var.myvar}"
  paramblah = "sdifjsdfssldkfjsldkfj"
  source = "...my git repo..."
}

module "mod3" {
  arg = "${var.myvar}"
  paramblah = "qiejiqjd"
  source = "...my git repo..."
}

output "myoutput" {
  value = ["${module.mod1.modsOutput}",
    "${module.mod2.modsOutput}",
    "${module.mod3.modsOutput}"]
}

output "mod1" { value = "${module.mod1.modsOutput}"}
output "mod2" { value = "${module.mod2.modsOutput}"}
output "mod3" { value = "${module.mod3.modsOutputs}"}

这似乎有效,但有没有办法让它更有活力?而不是必须冗余地插入它们?