variable

原文


注意:此页面是关于 Packer 的 HCL2 模板的。 HCL2 模板最初作为 Beta 功能被 Packer 1.5 版引入。从 v1.7 开始,HCL2 支持不再处于测试阶段,并且是编写 Packer 配置的首选方式。对于稳定的旧风格配置语言,请参阅模板文档。从 v1.6.2 开始,您可以使用 hcl2_upgrade 命令将遗留的 JSON 模板转换为 HCL2 配置文件。

variable 块,也称为 input-variable 块,用以在 Packer 配置中定义变量。在一个输入变量中不能使用另一个输入变量,我们建议改用本地变量

# variables.pkr.hcl
variable "foo" {
    type        = string
    default     = "the default value of the `foo` variable"
    description = "description of the `foo` variable"
    sensitive   = false
    # When a variable is sensitive all string-values from that variable will be
    # obfuscated from Packer's output.
}

默认值

如果设置了默认值,则该变量是可选的。否则,必须设置变量的值。

为输入变量赋值

有以下几种为配置文件中声明的输入变量赋值的方法:

  • -var foo=bar 这样的命令行参数对一个输入变量赋值
  • 使用变量文件,要么用 -var-files values.pkrvars.hcl 这样的命令行参数,要么使用自动加载的变量文件(文件名以 *.auto.pkrvars.hcl 为后缀)
  • 使用环境变量,例如:PKR_VAR_foo=bar

定制输入参数校验规则

除了类型约束之外,您还可以在 variable 块中定义一个或多个 validation 块为特定变量指定任意自定义验证规则:

variable "image_id" {
  type        = string
  description = "The ID of the machine image (AMI) to use for the server."

  validation {
    condition     = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
    error_message = "The image_id value must be a valid AMI ID, starting with \"ami-\"."
  }
}

condition 参数是一个表达式,如果值有效则必须使表达式返回 true,否则返回 false。表达式只能引用声明该 validation 块的变量,并且不能产生错误。

如果要用表达式是否失败是验证参数的有效性,请使用 can 函数来检测此类错误。例如:

variable "image_id" {
  type        = string
  description = "The ID of the machine image (AMI) to use for the server."

  validation {
    # regex(...) fails if it cannot find a match
    condition     = can(regex("^ami-", var.image_id))
    error_message = "The image_id value must be a valid AMI ID, starting with \"ami-\"."
  }
}

如果 condition 的计算结果为 false,将产生一条错误消息,其中包含 error_message 中给出的句子。错误消息字符串应该是一个完整地解释约束为什么失败的句子,使用与上述示例类似的句子结构。

验证也适用于更复杂的情况:

variable "image_metadata" {

  default = {
    key: "value",
    something: {
      foo: "bar",
    }
  }

  validation {
    condition     = length(var.image_metadata.key) > 4
    error_message = "The image_metadata.key field must be more than 4 runes."
  }

  validation {
    condition     = can(var.image_metadata.something.foo)
    error_message = "The image_metadata.something.foo field must exist."
  }

  validation {
    condition     = substr(var.image_metadata.something.foo, 0, 3) == "bar"
    error_message = "The image_metadata.something.foo field must start with \"bar\"."
  }

}

上面的例子可以搭配这样一个变量文件:

# foo.pkrvars.hcl
foo = "value"

Packer 执行时输入变量值必须是已知的

假设这样一个输入变量:

variable "foo" {
  type = string
}

我们必须为 foo 设定一个值,但也可以将其 default 设置为 null 从而不需要设置值:

no default default = null default = "xy"
foo unused error, "foo needs to be set" - -
var.foo error, "foo needs to be set" null¹ xy
PKR_VAR_foo=yz
var.foo
yz yz yz
-var foo=yz
var.foo
yz yz yz

null 是一个合法值。Packer 只会在一个输入变量没有值的时候报错,例如:

variable "example" {
  type = string
  default = null
}

source "example" "foo" {
  arg = var.example
}

在上面的例子里,只要 arg 在使用 example 这个 source 时是可选的,那不对 arg 设置值就不会有错误。

隐藏敏感输入变量值

当变量包含敏感信息时,来自该变量的所有字符串值将在 Packer 的输出中进行混淆:

# var-foo.pkr.hcl
variable "foo" {
    sensitive = true
    default   = {
        key = "SECR3TP4SSW0RD"
    }
}
$ packer inspect var-foo.pkr.hcl
Packer Inspect: HCL2 mode

> input-variables:
var.foo: "{\n  \"key\" = \"<sensitive>\"\n }"
...

results matching ""

    No results matching ""