source 块
注意:此页面是关于 Packer 的 HCL2 模板的。 HCL2 模板最初作为 Beta 功能被 Packer 1.5 版引入。从 v1.7 开始,HCL2 支持不再处于测试阶段,并且是编写 Packer 配置的首选方式。对于稳定的旧风格配置语言,请参阅模板文档。从 v1.6.2 开始,您可以使用 hcl2_upgrade 命令将遗留的 JSON 模板转换为 HCL2 配置文件。
顶层 source 块定义了可重用的 builder 块配置:
# sources.pkr.hcl
source "happycloud" "foo" {
// ...
}
第一个标签 happycloud 是构建器类型。第二个标签是您要赋予 source 的独特的名称或标识符。地址为 source.happycloud.foo 的顶级 source 块只能有一个;但它可以多次使用。除了将保留在 Packer 核心中的 file 和 null 构建器以外,其他构建器通常由插件提供。
您可以通过从 build 块中引用这些 source 块来启动构建器:
build {
sources = [
# Here Packer will use a default ami_name when saving the image.
"source.happycloud.example",
"source.happycloud.foo",
]
}
build 级的 source 块允许设置指定的 source 参数。每个参数只能赋值一次,目前不允许使用 override。
build {
source "source.happycloud.example" {
# Here Packer will use the provided image_name instead of defaulting it.
# Note that fields cannot be overwritten, in other words, you cannot
# set the 'image_name' field in the top-level source block and here at the
# same time
image_name = "build_specific_field"
}
}
Source 变量
可以从 provisioner 块和 post-processor 块中访问 source 的 name 和 type:
source "null" "first-example" {
communicator = "none"
}
build {
name = "roles"
source "null.first-example" {
name = "consul"
}
source "null.first-example" {
name = "nomad"
}
source "null.first-example" {
name = "vault"
}
sources = ["null.first-example"]
provisioner "shell-local" {
inline = ["echo ${source.name} and ${source.type}"]
}
}
# This will echo something like:
#
# roles.null.consul: consul and null
# roles.null.nomad: nomad and null
# roles.null.vault: vault and null
# roles.null.first-example: first-example and null