opentofu: Allow specifying input variables as unknown in `tofu plan`
OpenTofu Version
any
Use Cases
This is mostly to serve multi-statefile planning. As you have multiple statefiles, with the outputs of one being the inputs of another, you will have a problem when making plans, esp. for the first execution, but also for future executions. This is generally used by OpenTofu orchestrators, like Terragrunt, but could also be part of a custom automation.
Attempted Solutions
Right now you have to pass bogus variables as input variables for the plan. Example problem surfaced by this is that if you want to use terragrunt run-all and first create a plan, and then want to apply that plan (comprising all statefiles) you might end up applying placeholder values of variables for statefiles down the chain. In other words, if you want to do it properly, you first have to plan+apply the first statefile, then the second, etc. in order for it to be correct.
Proposal
Allow marking input variables as unknown to tofu plan. This could be done by using a special argument like --unknown-variables abc,xyz. It would treat those variable values like any other unknown value in OpenTofu (as it very often treats output attributes of unapplied resources).
When applying a plan-file created with such unknown variables, you’d have to provide a value for each of them (like you normally would provide variable values).
This way, a tool like Terragrunt could actually create a planfile for the bootstrap run-all, but use unknown variables instead of placeholders. This way the resulting plan would be more accurate (showing unknown where unknown is what it is) and the apply of that planfile could actually specify the correct values later on (while still verifying that the apply corresponds to the planfile).
Notes
This ties in very well with our goal of improving how OpenTofu works with 3rd party tool authors.
It should also be a fairly trivial change (I’ll make a PoC once I have some time), as unknown values is something Tofu already handles all over the place, and this is just exposing it to the user as available for input variables.
Proof of Concept
In comment below: https://github.com/opentofu/opentofu/issues/812#issuecomment-1792802635
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 16
- Comments: 15 (13 by maintainers)
When you specify
-var-file foo.hcl, the idea is thatfoo.hclhas a bunch of key-value pairs to set input variables:By analogy, I guess
-var-file-unknownwould be just a list of the variable names:It’s just a way to reduce the number of args to pass on the CLI, especially for repetitive usage. That said, I’m not sure that a list of var names is valid HCL. Also, there’s some YAGNI risk here, so in the spirit of incrementalism, it might be fine to start with just
-var-unknown, see what real-world use cases arise, and add-var-file-unknownonly if requested/needed.Reacting to the prototype above, I wonder if it would be reasonable to say that unknown input variables are only allowed for
tofu plan(not fortofu apply) and that if you save a plan file where one of the input variables was unknowntofu applywould just refuse to apply it, similar to how we already refuse to apply a plan that is marked as having encountered a planning error. (Using a different error message for the new situation with unknown input variables, of course.)Input variables should presumably be unknown only if an upstream hasn’t been applied yet. Once the upstream has been applied, the orchestration tool should re-plan the downstream with the known results from that apply, and so the original speculative plan with unknown input variables becomes irrelevant.
As long as the orchestration tool is propagating values from one configuration to another in a way that’s consistent with how the main OpenTofu language behaves – that is, maintaining all invariants such as forcing known values in the speculative plan to have identical values in the final plan – the final plan would be consistent with the initial plan in the same way that OpenTofu Core itself relies on when propagating values between resources.
I generally agree that this would be useful. However, I have concerns about the overall design of the “tf” language. It was originally a language that was fully (or as much as possible) deterministic in its execution, which came with limitations. Limitations that the community has asked to get rid of because it blocked use cases for years.
However, it feels like a lot of the new features that lower the determinism of the language are, in and of themselves solid, but there’s no bigger picture where we want the language to be in 2-3-5 years time. There are quite a few languages that have gone through the process of adding individually cool features and then needing over a decade to make their internal behavior consistent again. For that reason alone I would like to recommend to hold off on features like this until we have a clear picture on what TF should look like.
Managed to do a successful PoC of this.
Here’s the diff: https://github.com/opentofu/opentofu/compare/kubas-unknown-variables-experiment
Here’s a recording:
https://github.com/opentofu/opentofu/assets/7013055/d098490d-1394-4c68-81c1-de3ad271c4cb
Overall, the hardest part is plumbing. The apply operation is structured fairly rigidly to either get variables from the user, or the plan (but never both), and that would require a bunch of changes.
@Yantrio Off the top of my head, using mocked values could lead you to miss-evaluating conditionals, for instance, which would make the planfile invalid with the actually-specified value.
You want the plan to treat it as unknown, so that all possible evaluation branches are allowed by the planfile.
Seems like a great idea.
Perhaps
-var-unknownand-var-file-unknownas the CLI args, to be a bit more consistent with-varand-var-file?I also wonder if there is some sort of
-var-unknown-allor similar flag that basically lets you runtofu planwithout having to specify any input variables? Might be handy for testing /validating things quickly, without getting caught up in specifying inputs, which can sometimes get complicated.