如何打印 terraform 变量值?

raj*_*eev 21 infrastructure terraform azure-devops azure-pipelines

我正在学习地形。我想在“计划”阶段打印变量的值。所以我找到了该怎么做。似乎我在这里做错了什么......

在变量.tf中:....

variable "VMCount" {
    description = "How many VMs do you want to start with (number)? default=1 max=5"
    type = number
}
Run Code Online (Sandbox Code Playgroud)

在 main.tf 中

output "VMCount" {
  value = "${var.VMCount > 2 && var.VMCount < 6 ? var.VMCount : 2}"
}
Run Code Online (Sandbox Code Playgroud)

之后我运行 terraform plan 并且条件似乎工作正常(它创建了正确数量的虚拟机)

但变量输出还没有到来。为什么?

$ terraform output
VMC = 56
Run Code Online (Sandbox Code Playgroud)

VMC 可能来自之前的一些尝试(我尝试了几件事)。

如何打印用户输入的值(变量)?

谢谢。

Krz*_*tof 16

我用这个测试过:

variable "VMCount" {
    description = "How many VMs do you want to start with (number)? default=1 max=5"
    type = number
}

output "VMCount" {
  value = "${var.VMCount > 2 && var.VMCount < 6 ? var.VMCount : 2}"
}
Run Code Online (Sandbox Code Playgroud)

而且效果很好。

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + VMCount = 4

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


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

Outputs:

VMCount = 4
PS C:\d\m\terraform\output-and-variable> terraform output
VMCount = 4
PS C:\d\m\terraform\output-and-variable> terraform apply
var.VMCount
  How many VMs do you want to start with (number)? default=1 max=5

  Enter a value: 8


Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:

Terraform will perform the following actions:

Plan: 0 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  ~ VMCount = 4 -> 2

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


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

Outputs:

VMCount = 2
PS C:\d\m\terraform\output-and-variable> terraform output
VMCount = 2
PS C:\d\m\terraform\output-and-variable>
Run Code Online (Sandbox Code Playgroud)

你能检查一下你所在州有哪些出局吗?VMC 还是 VMCount?


Ben*_*Ben 6

正如terraform 文档中所述:

仅当 Terraform 应用您的计划时才会渲染输出。运行 terraform plan 将不会渲染输出。