For example I want to use a variable for reuse inside a variables block:
"variables": {
"worker_shortname": "Archival",
"component_name": "{{user `worker_shortname`}}Worker"
}
Unfortunately this seems to be not supported with the current packer (0.5.1) version, but I get the following error message:
Error processing user variable 'component_name': template: tpl0:1:2: executing "tpl0" at <user `worker_shortna...>: error calling user: User variable can't be used within a default value for a user variable: worker_shortname'
This would be a really handy enhancement; it would allow for significantly more DRY templates here!
This is a purposeful limitation. Supporting this would require a dependency graph and a lot more complication, and limiting this is a simplification. Sorry!
It seems if there was a requirement that a composite variable was only allowed later in the variable block than the variables it was dependent on it would not require a dependency graph. The way it fails now (silently replacing the variable with nothing) isn't very helpful or obvious.
Yeah, couldn't packer validate
catch this?
With Packer v1.0.0 i get more confusing error message:
Error initializing core: error interpolating default value for 'vm_name': template: root:1:9: executing "root" at <user `centos_version...>: error calling user: test
Would be REALLY helpful if this feature was implemented. :pray:
Ran into this issue today. You can get around this by using the racker preprocessor.
iso_url = 'http://cdimage.ubuntu.com/releases/18.04/release/ubuntu-18.04.2-server-amd64.iso'
Racker::Processor.register_template do |t|
t.variables = {
'iso_url' => iso_url,
'notes' => "Built on {{ timestamp }} using ${iso_url}",
...
Then
racker ubuntu.rb - | packer build -timestamp-ui -
or
racker ubuntu.rb - > ubuntu.json && packer build -timestamp-ui ubuntu.json
@nitrocode FYI this feature is now available within Packer directly :).
Hmmm. I tried in 1.3.5 and had issues so I had to go the racker route. I'll retry with the latest 1.4.2.
Nice, didn't realize this shipped! Found the mention in the changelog for 1.4.0:
core: Allow user variables to be interpreted within the variables section [GH-7390]
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
It seems if there was a requirement that a composite variable was only allowed later in the variable block than the variables it was dependent on it would not require a dependency graph. The way it fails now (silently replacing the variable with nothing) isn't very helpful or obvious.