【问题标题】:How to print terraform variable values?如何打印地形变量值?
【发布时间】:2021-09-01 17:38:22
【问题描述】:

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

在变量.tf:....

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

在 main.tf 中

output "VMCount" {
  value = "${var.VMCount > 2 && var.VMCount < 6 ? var.VMCount : 2}"
}

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

但是变量输出没有出现。为什么?

$ terraform output
VMC = 56

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

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

谢谢。

【问题讨论】:

  • 您的代码与显示的输出不匹配。具体来说,如果您将 VMCount 设置为 56,它将当前返回 2。您能否编辑您的问题以显示您的实际代码(最好是 minimal reproducible example)以及运行 terraform applyterraform output 时获得的实际输出?

标签: azure-devops terraform infrastructure


【解决方案1】:

我用这个测试过:

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}"
}

而且效果很好。

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>

你能检查一下你在州里有什么outouts吗? VMC 还是 VMCount?

【讨论】:

  • 如何检查?
  • 你有远程后端或者你在哪里存储你的状态?打开状态文件,您应该会在顶部附近找到输出。
猜你喜欢
  • 2015-01-22
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多