Merge pull request #8026 from hfinucane/jinja-args
Support a whitelisted subset of jinja2 template options
This commit is contained in:
commit
1e05688b04
1 changed files with 14 additions and 0 deletions
|
@ -80,6 +80,8 @@ class Flags:
|
|||
|
||||
FILTER_PLUGINS = None
|
||||
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
||||
JINJA2_OVERRIDE = '#jinja2:'
|
||||
JINJA2_ALLOWED_OVERRIDES = ['trim_blocks', 'lstrip_blocks', 'newline_sequence', 'keep_trailing_newline']
|
||||
|
||||
def lookup(name, *args, **kwargs):
|
||||
from ansible import utils
|
||||
|
@ -230,6 +232,18 @@ def template_from_file(basedir, path, vars, vault_password=None):
|
|||
except:
|
||||
raise errors.AnsibleError("unable to read %s" % realpath)
|
||||
|
||||
# Get jinja env overrides from template
|
||||
if data.startswith(JINJA2_OVERRIDE):
|
||||
eol = data.find('\n')
|
||||
line = data[len(JINJA2_OVERRIDE):eol]
|
||||
data = data[eol+1:]
|
||||
for pair in line.split(','):
|
||||
(key,val) = pair.split(':')
|
||||
key = key.strip()
|
||||
if key in JINJA2_ALLOWED_OVERRIDES:
|
||||
setattr(environment, key, ast.literal_eval(val.strip()))
|
||||
|
||||
|
||||
environment.template_class = J2Template
|
||||
try:
|
||||
t = environment.from_string(data)
|
||||
|
|
Loading…
Reference in a new issue