使用OpenStack。我有一个用Packer构建镜像的两个步骤的过程: (1) 使用Terraform创建基础架构,基本上只是将网络路由到互联网,以及允许SSH的一些安全组。 (2) 使用Packer构建镜像
问题是我需要提供由Terraform构建的网络ID给Packer。我可以通过检查状态文件手动完成此操作,但我想知道自动化此过程的最佳实践是什么?
packer build -var network=$(terraform output network_uuid) template.json
resource "null_resource" "packer_runner" {
triggers = {
install_script = "${sha1(file("${path.module}/scripts/app/install.sh"))}"
packer_file = "${sha1(file("${path.module}/packer/packer.json"))}"
}
provisioner "local-exec" {
working_dir = "${path.module}"
command = "packer build -var 'ami_name=${var.ami_name}' -var 'aws_region=${var.aws_region}' -var 'network_id=${var.network_id}' -var -parallel-builds=1 ./packer/packer.json"
interpreter = ["PowerShell", "-Command"]
}
}
packer.json
文件中:<...stuff...>
"provisioners": [
{
"type": "shell",
"inline": "/usr/bin/cloud-init status --wait"
},
{
"type": "shell",
"environment_vars": [
"NETWOR_ID={{user `network_id`}}"
],
"script": "./scripts/app/install.sh"
},
<...more stuff...>
想作为一个建议添加到上面的答案中,但编辑队列已满。然后单独添加。
如果您需要将terraform输出的所有变量传递给packer的“input”:
packer build $(terraform output -json | jq -j -r 'to_entries[] | " -var \(.key)=\(.value | .value)"') template.pkr.hcl
terraform output --raw
命令可以避免字符串变量周围出现不必要的引号。 - hertzsprung