如何从 terraform 状态设置打包程序变量
How to set a packer variable from a terraform state
使用 openstack。我有两个步骤来使用加壳器构建图像:
(1) 基本上使用 terraform 创建基础设施,只是一个路由到互联网的网络和一些允许 SSH 的安全组
(2) 使用 packer
构建镜像
问题是我需要将 terraform 构建的网络的 id 提供给加壳器。我可以通过检查状态文件手动执行此操作,但我想知道自动执行此操作的最佳做法是什么?
您可以使用 terraform output 来读取状态的输出。您可以将它们作为 Packer 变量传递,即
packer build -var network=$(terraform output network_uuid) template.json
另一个建议:您可以从 Terraform 调用 Packer。
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 输出传递到加壳器的 'input':
packer build $(terraform output -json | jq -j -r 'to_entries[] | " -var \(.key)=\(.value | .value)"') template.pkr.hcl
使用 openstack。我有两个步骤来使用加壳器构建图像: (1) 基本上使用 terraform 创建基础设施,只是一个路由到互联网的网络和一些允许 SSH 的安全组 (2) 使用 packer
构建镜像问题是我需要将 terraform 构建的网络的 id 提供给加壳器。我可以通过检查状态文件手动执行此操作,但我想知道自动执行此操作的最佳做法是什么?
您可以使用 terraform output 来读取状态的输出。您可以将它们作为 Packer 变量传递,即
packer build -var network=$(terraform output network_uuid) template.json
另一个建议:您可以从 Terraform 调用 Packer。
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 输出传递到加壳器的 'input':
packer build $(terraform output -json | jq -j -r 'to_entries[] | " -var \(.key)=\(.value | .value)"') template.pkr.hcl