Automating Prefix Assignments with Netbox


I am running into a logic issue, understanding that Terraform is declarative, but in this case we are limiting our declaration.

To iterate through a list of parent prefixes until one a parent prefix is found that can accommodate the new subnet prefix.

I need to use a data call to look at a prefix and determine if it can handle the new subnet prefix in the resource block. I have a list of parent prefix’s that have been carved out and can be used to create the new subnet prefix (it doesn’t matter which one, just the first available with enough space). The issue is the resource block errors when the parent prefix does not have enough address space to accommodate the new subnet prefix. While this is expected, I’m unsure how I can have the resource try to create using the next parent prefix in the list.

I am currently using for_each on the data calls (one for each parent prefix). This allows me to pass each parent prefix to the resource block, but I do not want to create a new subnet prefix in every single parent prefix (I only want to create in the first available parent prefix that has the available space).

Can provide more detail if needed, but my research thus far has led me to believe this type of conditional resource creation is not possible without some 3rd party service/tool.


For anyone wondering, the workaround was utilizing a Provisioner.