Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »


Rendering is changing data into something <variable> ------->  into "value". 

Parsing takes raw data or input and converts it a data structure, or structures that can be analyzed. 


Jinja2 - 101 - Intro to Jinja2 template building


Manual:   https://media.readthedocs.org/pdf/jinja2/latest/jinja2.pdf





jinja2 module
 With Python: ( use extensively by Ansible)

>>> from jinja2 import Template

>>> template = Template('Hello {{ name }}!')

>>> template.render(name='John Doe')

'Hello John Doe!'

render jinja2 : option 1
from jinja2 import Template

def render_jinja2():
    with open(r"C:\Users\jkriker\Documents\GitHub\JAUT_Training\Basic-python-with-Juniper\Napalm\config_unit.j2") as file_:
        template = Template(file_.read())
    print(template.render(unit_id='5', vlan_id=5, subnet="0.5"))


render_jinja2(template_name, list_variables)


option 2
import jinja2

def render_j2():
    templateLoader = jinja2.FileSystemLoader(searchpath = r"C:\Users\jkriker\Documents\GitHub\JAUT_Training\Basic-python-with-Juniper\Napalm" )
    templateEnv = jinja2.Environment(loader=templateLoader)
    TEMPLATE_FILE = "config_unit.j2"
    template = templateEnv.get_template(TEMPLATE_FILE)
    outputText = template.render(unit_id=5, vlan_id=5, subnet="0.5")
    print(outputText)










  • No labels