Enable optional config template selection on Device "Render Config" #55

Open
opened 2026-04-05 16:21:22 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @sweston99 on 3/30/2026

NetBox version

v4.5.3

Feature type

Change to existing functionality

Proposed functionality

The proposal here is to read the url for a variable that specifies a config template to use; if none is provided, default to current behaviour, i.e. use the template assigned to the device/role/platform. However, if a config template is specified, use that one instead. This behaviour should also apply to the APIs as well.

/dcim/devices/10185/render-config/
/dcim/devices/10185/render-config/?config_template_id=23

This would require the following changes to ObjectRenderConfigView::get_extra_context() to read the request object and, if a template is specified in the request, use that rather than the default one attached to the device instance.

def get_extra_context(self, request, instance):
    # Compile context data
    context_data = instance.get_config_context()
    context_data.update(self.get_extra_context_data(request, instance))

    # Render the config template
    rendered_config = None
    error_message = ''

    # if an optional config template has been specified, use that
    if config_template_id := request.GET.get('config_template_id'):
        config_template = ConfigTemplate.objects.get(pk = config_template_id)
    else:
        config_template = instance.get_config_template()

    if config_template:
        try:
            rendered_config = config_template.render(context=context_data)
        except TemplateError as e:
            error_message = _("An error occurred while rendering the template: {error}").format(error=e)

    return {
        'base_template': self.base_template,
        'config_template': config_template,
        'context_data': context_data,
        'rendered_config': rendered_config,
        'error_message': error_message,
    }

Additionally, changes required to the UI are required to allow the user to select which template to use. Default behaviour could remain as is, selecting the Render Config tab renders the default config template; a simple form could be included on the Render tab to allow the user to optionally select a different config template.

Use case

Sometimes it is desirable to be able to apply a partial config template to a device context and get the resulting rendered config, for example, changing only ntp server configs where the variables depend on the region that the device is in. This is especially needed where the modelled network does not equal the actual network and so the fully rendered configs cannot be blindly applied, where a partial config can be (as in the ntp change example).

This will typically be used to drive network automations to close network drift, e.g. the ntp changes above would be driven by Ansible automations that retrieve a device config from netbox by specifying both the device and the template to use.

Database changes

None required

External dependencies

No response

*Originally created by @sweston99 on 3/30/2026* ### NetBox version v4.5.3 ### Feature type Change to existing functionality ### Proposed functionality The proposal here is to read the url for a variable that specifies a config template to use; if none is provided, default to current behaviour, i.e. use the template assigned to the device/role/platform. However, if a config template is specified, use that one instead. This behaviour should also apply to the APIs as well. /dcim/devices/10185/render-config/ /dcim/devices/10185/render-config/?config_template_id=23 This would require the following changes to ObjectRenderConfigView::get_extra_context() to read the request object and, if a template is specified in the request, use that rather than the default one attached to the device instance. def get_extra_context(self, request, instance): # Compile context data context_data = instance.get_config_context() context_data.update(self.get_extra_context_data(request, instance)) # Render the config template rendered_config = None error_message = '' # if an optional config template has been specified, use that if config_template_id := request.GET.get('config_template_id'): config_template = ConfigTemplate.objects.get(pk = config_template_id) else: config_template = instance.get_config_template() if config_template: try: rendered_config = config_template.render(context=context_data) except TemplateError as e: error_message = _("An error occurred while rendering the template: {error}").format(error=e) return { 'base_template': self.base_template, 'config_template': config_template, 'context_data': context_data, 'rendered_config': rendered_config, 'error_message': error_message, } Additionally, changes required to the UI are required to allow the user to select which template to use. Default behaviour could remain as is, selecting the Render Config tab renders the default config template; a simple form could be included on the Render tab to allow the user to optionally select a different config template. ### Use case Sometimes it is desirable to be able to apply a partial config template to a device context and get the resulting rendered config, for example, changing only ntp server configs where the variables depend on the region that the device is in. This is especially needed where the modelled network does not equal the actual network and so the fully rendered configs cannot be blindly applied, where a partial config can be (as in the ntp change example). This will typically be used to drive network automations to close network drift, e.g. the ntp changes above would be driven by Ansible automations that retrieve a device config from netbox by specifying both the device and the template to use. ### Database changes None required ### External dependencies _No response_
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#55