Rendering Jinja templates
Infrahub can natively render any Jinja templates dynamically. Internally it's referred to as Jinja2 Transform
.
Please refer to Transform to learn more about it.
Generate the configuration of a device
The rendered configuration is available via the REST API under /api/transform/jinja2/<transform_name>
followed by any additional parameters expected in the GraphQL query.
The Jinja2 transform, device_startup
, present in the repository expects the name of the device as a parameter /api/transform/jinja2/<transform_name>?device=<device_name>
. As an example, below is the URL for couple of devices:
- Configuration for
ord1-edge1
(/api/transform/jinja2/device_startup?device=ord1-edge1
) - Configuration for
atl1-edge2
(/api/transform/jinja2/device_startup?device=atl1-edge2
)
In these examples device_startup
is the name of an transform defined in the infrahub-demo-edge
repository. The query string ?device=atl1-edge2
includes all the arguments that are required by the GraphQL query associated with this transform.
Create a new branch, then change the data AND the template
Next, we'll create a new branch and make modifications both in the data and in the template to explore the integration between the Jinja Template Renderer and the storage engine.
1. Create a new branch update-ethernet1
From the frontend, create a new branch named update-ethernet1
and be sure to uncheck the toggle Is data only
in the UI.
2. Update the interface Ethernet 1 for atl1-edge1
Now we'll make a change in the branch update-ethernet1
that will be reflected in the rendered template, like updating the documentation.
- Navigate to the device
atl1-edge1
in the frontend - Navigate to the list of its interfaces in the
Interfaces
Tab - Select the interface
Ethernet1
- Edit the interface
Ethernet
- Update its description to
New description in the branch
- Save your change
3. Update the Jinja2 template in GitHub
The final step is to modify the Jinja template directly from GitHub
In GitHub:
- Navigate to your clone
- Select the new branch in the branch menu dropdown
- Select the file
templates
/device_startup_config.tpl.j2
- Edit the file with the
pen
in the top right corner - Delete the lines 77 and 78 (i.e. the last two lines of
ip prefix-list BOGON-Prefixes
) - Commit your changes in the branch
update-ethernet1
directly from GitHub
After making these changes, you should be able to render the transform for the branch update-ethernet1
and see the changes made to the data AND to the schema at the same time at the address /api/transform/jinja2/device_startup?device=atl1-edge1&branch=update-ethernet1
4. Merge the Branch update-ethernet1
After merging the branch update-ethernet1
, regenerate the configuration for atl1-edge1
in main
and validate that the 2 changes are now available in main
.