-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
for_each causes terraform crash when creating null_resource #22580
Comments
Turns out I can accomplish the same (dynamic resource creation) using For example: main.tf
variable "subnet_id" {
description = "IP Subnet specified in CIDR notation (e.g. 10.20.102.0/24) "
}
variable "host_range" {
description = "A list specifying the available IPs"
type = "list"
}
locals {
guest_ips = [for i in var.host_range : cidrhost(var.subnet_id, i)]
}
resource "null_resource" "guest_ips" {
triggers = {
all_guest_ips = element(local.guest_ips, count.index)
}
count = length(local.guest_ips)
} Here is the calling module: module "subnet_1" {
source = "../../../../soltest-terraform-modules/modules/libvirt/network/ip_range"
subnet_id = "10.20.102.0/24"
host_range = flatten([range(1,5), range(11,15)])
} I usually do not own an entire subnet, and must share it amongst other teams (not ideal, of course). This approach allows me to specify exactly which hosts are available in a given subnet. |
Hi @muroj In your case, i think it´s acomplise with this code: variable "subnet_id" {
description = "IP Subnet specified in CIDR notation (e.g. 10.20.102.0/24) "
default = "10.20.102.0/24"
}
locals {
guest_ips = { for i in range(1, 255) : i => cidrhost(var.subnet_id, i) }
}
resource "null_resource" "guest_ips" {
for_each = local.guest_ips
triggers = {
all_guest_ips = each.value
}
} I have some issues with for_each in resources, and i dont understant how its works. |
Thanks @jcmaasb, your code snippet works. However, I am not clear on benefits of this approach. Why would I use |
Hi @jcmaasb, I wrote a little more about what For this issue (thanks @muroj for opening!) it appears it has to do with unknown values. We should not panic here, and will fix that. The rule for using expressions with count (values must be known) should apply to Edit for further clarification: The value is unknown at one point during the plan, but becomes known. Bug found and will fix, it looked like your code should work :) |
Hi @pselle |
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. |
Terraform Version
Terraform Configuration Files
Calling module:
Crash Output
https://gist.github.com/muroj/35b5d7ca81cdf96fe81f37e4773f4013
Expected Behavior
A null resource for each IP should have been created.
Actual Behavior
terraform crashes
Steps to Reproduce
terraform init
terraform plan/apply
References
Similar to #22560
The text was updated successfully, but these errors were encountered: