-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not able to pass agent's environment variable [ env()] to array in variable default definition #12973
Comments
Hey @trlvsk, I guess coalesce may do the trick for you? Something like This would return the first non-empty string value among the arguments passed to coalesce, so from my understanding that'd work for you as if the env doesn't define the value you're looking for, you'll use the next one which would be your chosen default. Please correct me if I missed something, and we can discuss this more. |
Hello @lbajolet-hashicorp , Thanks for your time and reply.
When those values are hardcoded (either in main packer.hcl or in packer-config.hcl) then everything is working just fine. The problem only occurs while trying to pass env variable with env() to the list ( Variable type = list(string)) Thanks in advance :) |
Hi again @trlvsk, I think I misunderstood your question in the first place, you're wondering if you can use expressions for a default value, is that it? I've justed tested this in my environment for reference: variable test_values {
type = list(string)
default = [env("SHELL"), env("PWD")]
}
source "null" "test" {
communicator = "none"
}
build {
sources = ["null.test"]
provisioner "shell-local" {
inline = ["echo 'SHELL=${var.test_values[0]}'", "echo 'PWD=${var.test_values[1]}'"]
}
} Running the build yields the following:
So yes, you can use simple expressions, including Does that answer your question? |
Hello again @lbajolet-hashicorp, Thanks for the above answer. It turns out that issue was not in the variable definition but in the way variable was called. Before I was calling variable as below which was failing as explained in previous comments.
I removed the condition of var.share_ami from the source and it finally worked the way it should.
All in all what i have is enough for me to work with but would like to know if it's expected to behave like this or that's some bug. |
Hi @trlvsk, To be clear, I see you're using a ternary op for both I've tried to replicate something like what you're trying to do, I don't see why that should be different, but it could have something to do with the context so I'm not ruling out definitely the possibility of a bug here. For reference, here's the template I run my test on: variable check {
type = bool
default = false
}
variable test_values {
type = list(string)
default = [env("SHELL"), env("PWD")]
}
local "end_arr" {
expression = var.check ? var.test_values : ["abc", "def"]
}
source "null" "test" {
communicator = "none"
}
build {
sources = ["null.test"]
provisioner "shell-local" {
inline = ["echo 'SHELL=${local.end_arr[0]}'", "echo 'PWD=${local.end_arr[1]}'"]
}
} And here's what I get depending on the value of
At first glance this looks like what you're doing besides the location of the expression being in a |
Hello again. Apologies for time it took to reply. I've tested it again with the right condition which is share_ami set to true and indeed it works. Closing the issue :) |
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. |
Community Note
When filing a bug, please include the following headings if possible. Any
example text in this template can be deleted.
Overview of the Issue
I'm working with bamboo pipelines and using it's agents with a lot of environment variables.
So far I've been passing multiple of those using env() function such as names, architecture, technology, dynamic paths, versions, and many other which to this point was working like a charm - Example below.
Recently due to redefinition on my organization AWS accounts i need to pass to ami_users variable 3 separate ID's (Dev / Pre-Prod / Prod)
There are more than 20 different accounts and each of those 20 have their own (Dev / Pre-Prod / Prod).
ID's for those 3 accounts are always stored within 3 separate bamboo variables.
I'm having trouble though to add them into the default array while defining ami_users variable
ami_users (or rather list of shared accounts) after creation of AMI is empty
The ultimate question is if Packer allows adding default array using env() function or not as documentation does not specify that. (If that is possible I'd love to see where am I wrong below.)
Reproduction Steps
When pipeline is ran with AWS account ID's hardcoded either in packer.hcl or in the packer-config then AMI is created perfectly fine and shared to those accounts.
When I run it with defaults only the AMI is not shared with no accounts.
Bamboo variables itself are not the problem in this case because they work like a charm for every other variable.
The issue is when I need to pack those into an array.
Below pasting two approaches that passed the builds but in the end did not share the ami to desired accounts.
In other words array passed is empty and AMI is not shared at all.
Packer Version
Packer 1.9.4
Operating system and Environment details
Debian Bookworm and/or AL2023
The text was updated successfully, but these errors were encountered: