Open johnson-earls opened 7 months ago
I use conditionals all the time in my project.
For example:
locals {
data_source_command = var.common_data_source == "http" ? "inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/ks.cfg" : "inst.ks=cdrom:/ks.cfg"
}
source "vsphere-iso" "linux-rhel" {
# ...
iso_paths = var.common_iso_content_library_enabled ? [local.iso_paths.content_library] : [local.iso_paths.datastore]
http_content = var.common_data_source == "http" ? local.data_source_content : null
cd_content = var.common_data_source == "disk" ? local.data_source_content : null
http_ip = var.common_data_source == "http" ? var.common_http_ip : null
http_port_min = var.common_data_source == "http" ? var.common_http_port_min : null
http_port_max = var.common_data_source == "http" ? var.common_http_port_max : null
# ...
boot_command = [
"<up>",
"e",
"<down><down><end><wait>",
"text ${local.data_source_command}",
"<enter><wait><leftCtrlOn>x<leftCtrlOff>"
]
# ...
}
Might be good to add such an example because it certainly can be used. 💯
Community Note
Overview of the Issue
The Expressions documentation at https://developer.hashicorp.com/packer/docs/templates/hcl_templates/expressions talks about "conditional evaluation" in a couple of places. But nowhere in that documentation or anywhere else in the Packer documentation do they give examples of or say how to do conditional evaluation. To me, conditional evaluation means logic that determines a value, something like C's
?:
ternary operator or Python'sfoo if bar else baz
conditional operator syntax. There is no such thing in Packer. The documentation should not claim there is and then not say anything else about it.Reproduction Steps
Read the documentation.
Packer version
Packer version 1.9 or 2.0
Simplified Packer Template
I don't have a template because the documentation to create such a template does not exist.
Operating system and Environment details
Not applicable.
Log Fragments and crash.log files
Not applicable.