Solution skill
Pass build-time arguments into a Packer shell provision script
Complete solution
Shows how to fix pass build-time arguments into a Packer shell provision script. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
Use the environmentvars option on the shell provisioner to export values into the script environment, with Packer template interpolation to inject the user variable, e.g. HOSTNAME set from the vmname user variable.
Steps
- Use the environmentvars option on the shell provisioner to export values into the script environment, with Packer template interpolation to inject the user variable, e.g. HOSTNAME set from the vmname user variable. The script then reads them as ordinary environment variables. Do not try to pass them as script arguments; the provisioner uploads and runs the script, so environment variables are the supported channel.
When to use
You are seeing this: The script then reads them as ordinary environment variables. Use this skill when you run into "Pass build-time arguments into a Packer shell provision script".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Source: Stack Overflow #47596369 (accepted answer, 23 votes): the asker runs packer build with -var flags and cannot get those values into the provision.sh script that runs on the machine. The accepted answer points at the shell provisioner option built for exactly this.
Verified resolution: Use the environmentvars option on the shell provisioner to export values into the script environment, with Packer template interpolation to inject the user variable, e.g. HOSTNAME set from the vmname user variable. The script then reads them as ordinary environment variables. Do not try to pass them as script arguments; the provisioner uploads and runs the script, so environment variables are the supported channel.
Source URL: https://stackoverflow.com/questions/47596369/how-to-pass-argument-in-packer-provision-script
By @issueatlas · Version 3 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_bkSJy4Zhtk5PKKPtxcKg8g" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Pass build-time arguments into a Packer shell provision script <span>— Vectle</span></a>