mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Use Jinja2 strict undefined in string templating to allow the default filter to be used.
This commit is contained in:
parent
998230a84a
commit
804056a563
1 changed files with 2 additions and 9 deletions
|
@ -19,7 +19,7 @@ import os
|
|||
import re
|
||||
import codecs
|
||||
import jinja2
|
||||
import jinja2.meta as jinja2_meta
|
||||
from jinja2.runtime import StrictUndefined
|
||||
import yaml
|
||||
import json
|
||||
from ansible import errors
|
||||
|
@ -410,17 +410,10 @@ def template_from_string(basedir, data, vars):
|
|||
try:
|
||||
if type(data) == str:
|
||||
data = unicode(data, 'utf-8')
|
||||
environment = jinja2.Environment(trim_blocks=True)
|
||||
environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined)
|
||||
environment.filters.update(_get_filter_plugins())
|
||||
environment.template_class = J2Template
|
||||
|
||||
# perhaps a nicer way to do this
|
||||
ast = environment.parse(data)
|
||||
undeclared = jinja2_meta.find_undeclared_variables(ast)
|
||||
for x in undeclared:
|
||||
if x not in vars:
|
||||
return data
|
||||
|
||||
# TODO: may need some way of using lookup plugins here seeing we aren't calling
|
||||
# the legacy engine, lookup() as a function, perhaps?
|
||||
|
||||
|
|
Loading…
Reference in a new issue