Automation

 View Only
last person joined: 10 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  Question on Loading a Jinja Template

     
    Posted 01-30-2022 07:24

    Hi.

     

    I have the following very basic Python script that uses a Jinja template file "scrap.j2" and instead of using a YAML file, I feed variables in the python script directly using a string.

     

    I am unable to successfully load the Jinja template. I get no error, but the result is an empty string.

     

    Here is my Python script

    #! /usr/bin/python3

    from jinja2 import Environment, FileSystemLoader

     

    #load Jinja Template

    env=Environment(loader=FileSystemLoader("./"))

    my_template = env.get_template("scrap.j2")

     

    #feed variables to the template

    result = my_template.render({"user": "lab"})

     

    print(result)

     

     

    Here is my Jinja file scrap.j2

    hello {{ user }}

     

     

     

    I expect the script to print "hello lab", but instead it's printing an empty string.

     

     

    Kindly let me know what I'm doing wrong here.

     

     

    Many thanks,

    Deepak


    Juniper Business Use Only



  • 2.  RE: Question on Loading a Jinja Template

     
    Posted 03-04-2022 15:05
    Hi Deepak,

    A quick copy/paste of your code with the associated jinja2 template works just fine for me.

    Maybe check versions of libraries, I was using Jinja2 3.0.1 and I also tested with 3.0.3 

    pip3 freeze | grep Jinja2
    Jinja2==3.0.3

    ./foo.py
    hello lab

    Regards

    ------------------------------
    Andy Sharp
    ------------------------------