post-processors
块
注意:此页面是关于 Packer 的 HCL2 模板的。 HCL2 模板最初作为 Beta 功能被 Packer 1.5 版引入。从 v1.7 开始,HCL2 支持不再处于测试阶段,并且是编写 Packer 配置的首选方式。对于稳定的旧风格配置语言,请参阅模板文档。从 v1.6.2 开始,您可以使用 hcl2_upgrade
命令将遗留的 JSON 模板转换为 HCL2 配置文件。
post-processors
允许我们定义一组 post-processor
,这些后处理器会搭配定义的 build
块的制品运行。
# builds.pkr.hcl
build {
# ...
post-processors {
post-processor "shell-local" { # create an artifice.txt file containing "hello"
inline = [ "echo hello > artifice.txt" ]
}
post-processor "artifice" { # tell packer this is now the new artifact
files = ["artifice.txt"]
}
post-processor "checksum" { # checksum artifice.txt
checksum_types = [ "md5", "sha512" ] # checksum the artifact
keep_input_artifact = true # keep the artifact
}
}
}
post-processors
块允许定义要为构建的制品运行的多个后处理器。阅读 post-processor
文档以了解如何使用后处理器。
post-processors
块与 post-processor
块的区别
这两个模板做的是相同的事:
# builds.pkr.hcl
build {
# ... build image
post-processor "checksum" { # checksum image
checksum_types = [ "md5", "sha512" ] # checksum the artifact
}
post-processor "amazon-import" { # upload image to AWS
}
post-processor "googlecompute-import" { # upload image to GCP
}
}
# builds.pkr.hcl
build {
# ... build image
post-processors {
post-processor "checksum" { # checksum image
checksum_types = [ "md5", "sha512" ] # checksum the artifact
}
}
post-processors {
post-processor "amazon-import" { # upload image to AWS
}
}
post-processors {
post-processor "googlecompute-import" { # upload image to GCP
}
}
}
所有这些 post-processors
都将在每次构建之后启动 —— 也就是说,在每个 source
上运行完所有 provisioner
步骤之后 —— 在所有情况下,源镜像都将被删除。