source
块
注意:此页面是关于 Packer 的 HCL2 模板的。 HCL2 模板最初作为 Beta 功能被 Packer 1.5 版引入。从 v1.7 开始,HCL2 支持不再处于测试阶段,并且是编写 Packer 配置的首选方式。对于稳定的旧风格配置语言,请参阅模板文档。从 v1.6.2 开始,您可以使用 hcl2_upgrade
命令将遗留的 JSON 模板转换为 HCL2 配置文件。
嵌套在 builder
块中的 source
块允许您使用已定义的顶级 source
块并“补充”那些尚未在顶级 source
块中设置的字段。
builder
级 source
块是通过将它们的内容与相应的顶级 source
块合并来实现的,当遇到 source
块某个参数有被定义了两次时,packer build
将会失败。例如,在下面的示例中,如果顶级 lxd.arch
source
块也定义了一个 output_image
字段(或者如果其中一个 builder
级的 source
块有 image
定义),Packer 将出错。
# file: builds.pkr.hcl
source "lxd" "arch" {
image = "archlinux"
}
build {
# Use the singular `source` block set specific fields.
# Note that fields cannot be overwritten, in other words, you cannot
# set the 'image' field from the top-level source block in here, as well as
# the 'name' and 'output_image' fields cannot be set in the top-level source block.
source "lxd.arch" {
# Setting the name field allows to rename the source only for this build section.
name = "nomad"
output_image = "nomad"
}
provisioner "shell" {
inline = [ "echo installing nomad" ]
}
}
build {
source "lxd.arch" {
name = "consul"
output_image = "consul"
}
provisioner "shell" {
inline = [ "echo installing consul" ]
}
}