mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Also load Jinja2 extensions when processing template from string
This commit is contained in:
parent
d7933522fb
commit
df3c4849e9
1 changed files with 20 additions and 15 deletions
|
@ -49,6 +49,23 @@ def _get_filters():
|
|||
|
||||
return Globals.FILTERS
|
||||
|
||||
def _get_extensions():
|
||||
''' return jinja2 extensions to load '''
|
||||
|
||||
'''
|
||||
if some extensions are set via jinja_extensions in ansible.cfg, we try
|
||||
to load them with the jinja environment
|
||||
'''
|
||||
jinja_exts = []
|
||||
if C.DEFAULT_JINJA2_EXTENSIONS:
|
||||
'''
|
||||
Let's make sure the configuration directive doesn't contain spaces
|
||||
and split extensions in an array
|
||||
'''
|
||||
jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',')
|
||||
|
||||
return jinja_exts
|
||||
|
||||
class Flags:
|
||||
LEGACY_TEMPLATE_WARNING = False
|
||||
|
||||
|
@ -375,22 +392,10 @@ def template_from_file(basedir, path, vars):
|
|||
realpath = utils.path_dwim(basedir, path)
|
||||
loader=jinja2.FileSystemLoader([basedir,os.path.dirname(realpath)])
|
||||
|
||||
'''
|
||||
if some extensions are set via jinja_extensions in ansible.cfg, we try
|
||||
to load them with the jinja environment
|
||||
'''
|
||||
jinja_exts = []
|
||||
if C.DEFAULT_JINJA2_EXTENSIONS:
|
||||
'''
|
||||
Let's make sure the configuration directive doesn't contain spaces
|
||||
and split extensions in an array
|
||||
'''
|
||||
jinja_exts = C.DEFAULT_JINJA2_EXTENSIONS.replace(" ", "").split(',')
|
||||
|
||||
def my_lookup(*args, **kwargs):
|
||||
return lookup(*args, basedir=basedir, **kwargs)
|
||||
|
||||
environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=jinja_exts)
|
||||
environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=_get_extensions())
|
||||
environment.filters.update(_get_filters())
|
||||
environment.globals['lookup'] = my_lookup
|
||||
|
||||
|
@ -445,12 +450,12 @@ def template_from_file(basedir, path, vars):
|
|||
return template(basedir, res, vars)
|
||||
|
||||
def template_from_string(basedir, data, vars):
|
||||
''' run a file through the (Jinja2) templating engine '''
|
||||
''' run a string through the (Jinja2) templating engine '''
|
||||
|
||||
try:
|
||||
if type(data) == str:
|
||||
data = unicode(data, 'utf-8')
|
||||
environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined)
|
||||
environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined, extensions=_get_extensions())
|
||||
environment.filters.update(_get_filters())
|
||||
environment.template_class = J2Template
|
||||
|
||||
|
|
Loading…
Reference in a new issue