I would like to pass in variables from python to jinja2 when rendering templates.
'with_remediation' is a boolean/varilable that I would like to pass into the jinja2 template for evaluation.
Here is how the jinja2 template file looks like:
{% if with_remediations %}
no hostname
{% endif %}
hostname {{ nodes.hostname }}
In my rendering function, I have these lines of code:
with_remediation = True
baseline = env.get_template(template)
config = baseline.render(nodes = node_object[index],with_remediation)
When I execute code, I get the following error:
config = baseline.render(nodes = node_object[index],with_remediation)
SyntaxError: non-keyword arg after keyword arg
However, if I specify the boolean directly into the function call:
config = baseline.render(nodes = node_object[index],with_remediation=True)
It runs without errors but not the expected output.
Here is the output when executed:
switch
/templates/cisco/ios/switch/test.jinja2
Push configs? [y/N]: y
config term
Enter configuration commands, one per line. End with CNTL/Z.
switch(config)#
switch(config)#
switch(config)#
switch(config)#
switch(config)#hostname switch
switch(config)#end
switch#
The expected result should be:
switch
/templates/cisco/ios/switch/test.jinja2
Push configs? [y/N]: y
config term
Enter configuration commands, one per line. End with CNTL/Z.
switch(config)#
switch(config)#no hostname
switch(config)#
switch(config)#
switch(config)#hostname switch
switch(config)#end
switch#
Why is it not executing the {% if with_remediations %} section since it holds true?