Hacker News new | past | comments | ask | show | jobs | submit login
Jinja 2.10 (github.com/pallets)
134 points by stablemap on Nov 8, 2017 | hide | past | favorite | 20 comments



The native environment should help ansible users a lot. As far as I know this will unblock quite a few issues that are currently open against ansible.

What it does: http://jinja.pocoo.org/docs/2.10/nativetypes/


Yeah. It has been a major pain for a lot of folks.

See https://github.com/pallets/jinja/pull/708 and its reference issues.


What’s a sample use case for this? A Python-ish VM to eval user supplied expressions?


The most common problem arises when your Ansible module's argument/option expects an integer for example, but because jinja (using filter such as arg="{{ my_var | do_something }}" returns a string, so the spec of your module will reject the input!

This is a hit or a miss in implementation. Dict, list, boolean, float, int, str are all acceptable primitives that must be handled properly.


Ansible generates out a lot of yaml and uses jinja for expressions.


While the changes in Jinja are welcome, maybe, just maybe, Ansible should consider not using yaml for absolutely everything. There can be more sensible ways to store data sometimes. Just saying.


> ... maybe, Ansible should consider not using yaml for absolutely everything.

Maybe. But what problems with Ansible are you trying to solve by using additional, and perhaps domain specific, languages?

I haven't used Ansible, but I do use SaltStack, which also relies on YAML, and also uses Jinja, hence my interest here.

SaltStack, and I'm assuming Ansible can do this too, will happily use whatever you want, even raw python, as state (configuration) files, so long as they can generate yaml.

This is not as inelegant as it sounds :) -- it means configuration length & complexity can be represented by (or hidden behind) short, palatable algorithms.

As soon as the suggestion is made to introduce yet another DSL to a system, you end up with holy wars about which one, and then a total mess trying to integrate and support both.

Anyway, if you don't want to use YAML / Jinja, then eschew SaltStack, Ansible etc in favour of Chef, Puppet, SCCM, etc.


You can use mako in salt, I haven't had any issues with it at all.


Mako and Jinja (until this release) really did not differ all that much with regards to this feature. However the ansible community outlined enough reasons for why they are interested in this.


Imagine an xmlrpc client module that could use any xmlrpc API and you wouldn't need one module for every task in the API. Which is the case now in the ansible modules.

But the module can't send an actual integer to the API if the API expects an int. It always came out as a string.

This is due to the issue with native types.

You could add logic to recognize integers and let users specify if they want integer conversion of values or not but that's a lot of extra crud for something that could be very simple.


I wish Python had more templating engines like Jade (now Pug), Slim & HAML. I can't go back to writing HTML anymore. I know about PyJade but it's (and all it's forks) seems always highly unmaintained.


I must say, I almost always try to swap in Mako for Jinja -- instead of learning yet another DSL for templating, I can just use a very good language I already know in my templates: Python.


You know Jinja compiles template in to python code, right ?


Yeah, what is your point?


For those of us unfamiliar with this stuff, would it be accurate think of a "template engine" as a sophisticated "printf"?


A templating engine does a few things differently than printf. Typically, it will compile into an intermediate version, for scripting languages like python as a module or object in the language. Most importantly, it escapes the input to prevent arbitrary HTML injection, for example a user inserting a <script> tag in a blog post content causing the javascript to be evaluated. Often, template engines have features for making it easier to design web user interfaces such as template inherritence and composition so you can share things like layouts, headers, and menus across pages.


+1. It can deal with non dev creating templates. It can compose templates. Manage caching. Embed logic. Debugging.


Little bit more than a single printf. For example you can do loops. https://stackoverflow.com/questions/17691838/range-in-jinja2...


sibling comments gave a much better explanation, but if you want to boil it down I think of it as python pretending to be PHP.


I wonder if the new namespaces objects can be (ab)used to return state from macros.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: