Configure undefined behavior for Jinja2 in Config Templates #8227

Closed
opened 2025-12-29 20:34:02 +01:00 by adam · 8 comments
Owner

Originally created by @clerie on GitHub (Jun 22, 2023).

NetBox version

v3.5.3

Feature type

Change to existing functionality

Proposed functionality

This is a request for an option to enable strict undefined behaviour in Jinja2 for Config Templates.

It is possible to provide environment params for Jinja2 to Config Templates. The Jinja2 provieds option undefined to choose the behaviour in case of missing attribues used in Jinja2 templates. The mentioned option requires a subclass of jinja2.Undefined as a value. It is not possible to pass Python classes using the JSON input type used by the environment params option in Netbox Config Templates.

Therefore I request a checkbox in the Config Templates edit form, which leads to Jinja2 undefined=jinja2.StrictUndefined in the Jinja2 environment while rendering the according template.

Use case

When working with data, especially with config contexts, you can't be always sure that every entry is available. Jinjas default behaviour is to silently ignore missing attributes and printing nothing instead. This is bad because this can result in incomplete config, which is mostly unnoticed in practice. Failing on missing attributes guarantees that templates do not render in that case and may prevent incomplete config in that stage.

Database changes

ConfigTemplate model in extras should get an additional boolean field environment_strict_unknown.

External dependencies

No response

Originally created by @clerie on GitHub (Jun 22, 2023). ### NetBox version v3.5.3 ### Feature type Change to existing functionality ### Proposed functionality This is a request for an option to enable strict undefined behaviour in Jinja2 for Config Templates. It is possible to provide environment params for Jinja2 to Config Templates. The Jinja2 provieds option `undefined` to choose the behaviour in case of missing attribues used in Jinja2 templates. The mentioned option requires a subclass of `jinja2.Undefined` as a value. It is not possible to pass Python classes using the JSON input type used by the environment params option in Netbox Config Templates. Therefore I request a checkbox in the Config Templates edit form, which leads to Jinja2 `undefined=jinja2.StrictUndefined` in the Jinja2 environment while rendering the according template. ### Use case When working with data, especially with config contexts, you can't be always sure that every entry is available. Jinjas default behaviour is to silently ignore missing attributes and printing nothing instead. This is bad because this can result in incomplete config, which is mostly unnoticed in practice. Failing on missing attributes guarantees that templates do not render in that case and may prevent incomplete config in that stage. ### Database changes `ConfigTemplate` model in `extras` should get an additional boolean field `environment_strict_unknown`. ### External dependencies _No response_
adam added the type: feature label 2025-12-29 20:34:02 +01:00
adam closed this issue 2025-12-29 20:34:02 +01:00
Author
Owner

@peterbaumert commented on GitHub (Jun 22, 2023):

Wouldnt it make more sense to use the jinja filter default to specify a value in the template itself. I for myself dont like "undefined" in my rendered configs either :D

@peterbaumert commented on GitHub (Jun 22, 2023): Wouldnt it make more sense to use the jinja filter default to specify a value in the template itself. I for myself dont like "undefined" in my rendered configs either :D
Author
Owner

@clerie commented on GitHub (Jun 23, 2023):

Wouldnt it make more sense to use the jinja filter default to specify a value in the template itself.

This would require a suitable default for that value. Sometimes that just doesn't exist. I.e. customer ID, default gateway and pppoe credentials.

@clerie commented on GitHub (Jun 23, 2023): > Wouldnt it make more sense to use the jinja filter default to specify a value in the template itself. This would require a suitable default for that value. Sometimes that just doesn't exist. I.e. customer ID, default gateway and pppoe credentials.
Author
Owner

@jeremystretch commented on GitHub (Jun 23, 2023):

This would require a suitable default for that value.

Not to be cheeky, but how about the string "undefined?"

@jeremystretch commented on GitHub (Jun 23, 2023): > This would require a suitable default for that value. Not to be cheeky, but how about the string "undefined?"
Author
Owner

@clerie commented on GitHub (Jun 30, 2023):

Using "undefined" as a default to a missing value would still render the config broken.

I think this breaks a bit with the concept of Jinja. Jinja tries to render anything if values are not available. For my configs I would like to have the option to only get an output if at least the input is complete. Incomplete configs are quite useless in my view.

@clerie commented on GitHub (Jun 30, 2023): Using "undefined" as a default to a missing value would still render the config broken. I think this breaks a bit with the concept of Jinja. Jinja tries to render anything if values are not available. For my configs I would like to have the option to only get an output if at least the input is complete. Incomplete configs are quite useless in my view.
Author
Owner

@jeremystretch commented on GitHub (Jul 3, 2023):

@clerie what are you proposing happen if the template then cannot be rendered?

@jeremystretch commented on GitHub (Jul 3, 2023): @clerie what are you proposing happen if the template then cannot be rendered?
Author
Owner

@clerie commented on GitHub (Jul 8, 2023):

Ideally it would show me all fields that are used within the template but do not contain any values. So I can then make sure these values exist.

@clerie commented on GitHub (Jul 8, 2023): Ideally it would show me all fields that are used within the template but do not contain any values. So I can then make sure these values exist.
Author
Owner

@jeremystretch commented on GitHub (Jul 10, 2023):

How? What specific workflow are you proposing?

@jeremystretch commented on GitHub (Jul 10, 2023): How? What specific workflow are you proposing?
Author
Owner

@jeremystretch commented on GitHub (Jul 26, 2023):

Closing this out as it's not clear what the proposed behavior would be and there's been no further response from OP.

@jeremystretch commented on GitHub (Jul 26, 2023): Closing this out as it's not clear what the proposed behavior would be and there's been no further response from OP.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8227