Add do extension to Jinja2 for Config Rendering #7954

Closed
opened 2025-12-29 20:30:27 +01:00 by adam · 2 comments
Owner

Originally created by @peterbaumert on GitHub (Apr 28, 2023).

NetBox version

v3.5.0

Feature type

New functionality

Proposed functionality

It would be great if the jinja2 do extension could be added, this would enable more flexibility in creating complex configuration templates.

Currently using do results in following error:

Traceback (most recent call last):
  File "/opt/netbox/netbox/dcim/views.py", line 2044, in get_extra_context
    rendered_config = config_template.render(context=context_data)
  File "/opt/netbox/netbox/extras/models/configs.py", line 252, in render
    output = template.render(**context)
  File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "switches/roles/access-switch.j2", line 1, in top-level template code
  File "switches/base.j2", line 2, in top-level template code
  File "switches/roles/access-switch.j2", line 5, in block 'content'
  File "/opt/netbox/netbox/utilities/jinja2.py", line 28, in get_source
    if referenced_templates := find_referenced_templates(environment.parse(template_source)):
  File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<unknown>", line 8, in template
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.

Use case

Writing more complex configuration templates.

Database changes

None

External dependencies

jinja2 do extension

Originally created by @peterbaumert on GitHub (Apr 28, 2023). ### NetBox version v3.5.0 ### Feature type New functionality ### Proposed functionality It would be great if the jinja2 do extension could be added, this would enable more flexibility in creating complex configuration templates. Currently using do results in following error: ``` Traceback (most recent call last): File "/opt/netbox/netbox/dcim/views.py", line 2044, in get_extra_context rendered_config = config_template.render(context=context_data) File "/opt/netbox/netbox/extras/models/configs.py", line 252, in render output = template.render(**context) File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render self.environment.handle_exception() File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "switches/roles/access-switch.j2", line 1, in top-level template code File "switches/base.j2", line 2, in top-level template code File "switches/roles/access-switch.j2", line 5, in block 'content' File "/opt/netbox/netbox/utilities/jinja2.py", line 28, in get_source if referenced_templates := find_referenced_templates(environment.parse(template_source)): File "/opt/netbox/venv/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "<unknown>", line 8, in template jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'. ``` ### Use case Writing more complex configuration templates. ### Database changes None ### External dependencies [jinja2 do extension](https://jinja.palletsprojects.com/en/3.0.x/extensions/#expression-statement)
adam added the type: feature label 2025-12-29 20:30:27 +01:00
adam closed this issue 2025-12-29 20:30:28 +01:00
Author
Owner

@kkthxbye-code commented on GitHub (Apr 28, 2023):

You can add environment parameters to the Config Template in the Environment params field.

{"extensions": ["jinja2.ext.do"]}

The above seems to work. As such, I don't think there's anything to do here. I'll close this, if I misunderstood or it doesn't work for you, let me know.

@kkthxbye-code commented on GitHub (Apr 28, 2023): You can add environment parameters to the Config Template in the `Environment params` field. `{"extensions": ["jinja2.ext.do"]}` The above seems to work. As such, I don't think there's anything to do here. I'll close this, if I misunderstood or it doesn't work for you, let me know.
Author
Owner

@peterbaumert commented on GitHub (Apr 28, 2023):

oh thank you, didn't find this in documentation

@peterbaumert commented on GitHub (Apr 28, 2023): oh thank you, didn't find this in documentation
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7954