azure-devops - 在 Terraform 中计算用户的输入?
问题描述
我想根据用户输入的数字使用 terraform 创建许多资源。数字必须在 2 到 5 之间。我试过:在vars.tf
:
variable "user_count" {
type = number
default = 2
description = "How many number of VMs to create (minimum 2 and no more than 5): "
}
这里的问题是它创建了默认编号为 2 的资源。
另一个案例:
variable "user_count" {
type = number
description = "How many number of VMs to create (minimum 2 and no more than 5): "
}
在这里,没有默认参数。我收到消息/描述,但我/用户可以输入任何内容!如何使这成为可能?- 用户收到消息并验证数字在 2 到 5 之间,否则将不会创建资源。
感谢您提供任何帮助-我真的陷入了困境!
解决方案
您可以尝试自定义验证
variable "user_count" {
type = number
description = "How many number of VMs to create (minimum 2 and no more than 5): "
validation {
condition = var.user_count > 1 && var.user_count < 6
error_message = "Validation condition of the user_count variable did not meet."
}
}
但也许更好的选择而不是检查数字,将可变为字符串和正则表达式来检查值是 2、3、4 还是 5。
variable "user_count" {
type = string
description = "How many number of VMs to create (minimum 2 and no more than 5): "
validation {
# regex(...) fails if it cannot find a match
condition = can(regex("2|3|4|5", var.user_count))
error_message = "Validation condition of the user_count variable did not meet."
}
}
推荐阅读
- r - 将带有因子的数据表的不规则时间序列转换为R中的规则时间序列
- reactjs - React App 将图像存储在公共文件夹中与从 src 文件夹中需要它们
- swift - 通过 FCM 发送数据消息,
- c++ - 堆缓冲区溢出与 std::deque、portaudio 和 AddressSanitizer
- python-3.x - 用于任务划分的多线程芹菜工作者
- sql - 用一个合并函数比较两列
- docker - wsl2上的docker非常慢
- corda - 带有令牌和帐户的 Corda
- java - Ionic 4、Android Studio、Gradle、Java jdk 或 Cordova 错误
- java - 从 docker 容器连接到主机 mysql db 时连接被拒绝