Reputation: 514
I'm trying to construct a template to support configuration lines for a specific console platform.
The running-configuration is in this following format:
Example 1:
/settings/network_connections/ETH0/ipv4_mode=static
/settings/network_connections/ETH0/pv4_address=10.0.0.10
/settings/network_connections/ETH0/ipv4_bitmask=24
/settings/network_connections/ETH0/ipv4_gateway=10.0.0.1
If you want to configure the following above lines, it's prepended by the word "set" like this:
Example 2:
set /settings/network_connections/ETH0/ipv4_mode=static
set /settings/network_connections/ETH0/pv4_address=10.0.0.10
set /settings/network_connections/ETH0/ipv4_bitmask=24
set /settings/network_connections/ETH0/ipv4_gateway=10.0.0.1
In my jinja2 template, I have something like this:
Example 3
{{ set| remediation }} /settings/network_connections/ETH0/ipv4_mode=static
{{ set| remediation }} /settings/network_connections/ETH0/pv4_address=10.0.0.10
{{ set| remediation }} /settings/network_connections/ETH0/ipv4_bitmask=24
{{ set| remediation }} /settings/network_connections/ETH0/ipv4_gateway=10.0.0.1
I want to be able to render the template and be able to output what I have in Example 2 (with 'set') as well as the ability to output as Example 1 (without 'set') using a boolean variable (with_remediation). If True, include 'set' - else exclude 'set'. In Example 3, 'remediation' being a embedded custom filter.
import jinja2
loader = jinja2.FileSystemLoader('/tmp')
env = jinja2.Environment(autoescape=True, loader=loader)
def remediation(input,with_remediation):
"""Custom filter"""
if(with_remediation):
return input
else:
return ""
env.filters['remediation'] = remediation
temp = env.get_template('template.jinja2')
temp.render(set="set")
But I'm unsure of how to pass the variable 'with_remediation' into the function remediate.
I have tried following the example Embed custom filter definition into jinja2 template? provided in the answer, but not sure if it will help in what I'm trying to achieve.
Also, how can I code it so 'set' can be any 'string' I want it to be? Must I include every string I want to use in the temp.render(set="set")
line? For example; temp.render(set="set", delete="delete",rename="rename")
or if there a more efficient way of tackling this?
Upvotes: 1
Views: 763
Reputation: 514
I was able to get resolve it by doing this; output 'set' by setting with_remediation to True; exlude output 'set' by setting with_remediation to False:
import jinja2
loader = jinja2.FileSystemLoader('/tmp')
env = jinja2.Environment(autoescape=True, loader=loader)
with_remediation = True
def remediation(input):
"""Custom filter"""
if(with_remediation):
return input
else:
return ''
env.filters['remediation'] = remediation
temp = env.get_template('template.jinja2')
temp.render(set='set')
Template:
{{ set | remediation }}/settings/network_connections/ETH0/ipv4_mode=static
{{ set | remediation }}/settings/network_connections/ETH0/pv4_address=10.0.0.10
{{ set | remediation }}/settings/network_connections/ETH0/ipv4_bitmask=24
{{ set | remediation }}/settings/network_connections/ETH0/ipv4_gateway=10.0.0.1
Upvotes: 1