|
|
|
@ -31,6 +31,7 @@ import datetime
|
|
|
|
|
import pwd
|
|
|
|
|
import ast
|
|
|
|
|
import traceback
|
|
|
|
|
from numbers import Number
|
|
|
|
|
|
|
|
|
|
from ansible.utils.string_functions import count_newlines_from_end
|
|
|
|
|
from ansible.utils import to_bytes, to_unicode
|
|
|
|
@ -81,6 +82,11 @@ class Flags:
|
|
|
|
|
|
|
|
|
|
FILTER_PLUGINS = None
|
|
|
|
|
_LISTRE = re.compile(r"(\w+)\[(\d+)\]")
|
|
|
|
|
|
|
|
|
|
# A regex for checking to see if a variable we're trying to
|
|
|
|
|
# expand is just a single variable name.
|
|
|
|
|
SINGLE_VAR = re.compile(r"^{{\s*(\w*)\s*}}$")
|
|
|
|
|
|
|
|
|
|
JINJA2_OVERRIDE = '#jinja2:'
|
|
|
|
|
JINJA2_ALLOWED_OVERRIDES = ['trim_blocks', 'lstrip_blocks', 'newline_sequence', 'keep_trailing_newline']
|
|
|
|
|
|
|
|
|
@ -109,7 +115,6 @@ def lookup(name, *args, **kwargs):
|
|
|
|
|
def template(basedir, varname, templatevars, lookup_fatal=True, depth=0, expand_lists=True, convert_bare=False, fail_on_undefined=False, filter_fatal=True):
|
|
|
|
|
''' templates a data structure by traversing it and substituting for other data structures '''
|
|
|
|
|
from ansible import utils
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if convert_bare and isinstance(varname, basestring):
|
|
|
|
|
first_part = varname.split(".")[0].split("[")[0]
|
|
|
|
@ -123,10 +128,13 @@ def template(basedir, varname, templatevars, lookup_fatal=True, depth=0, expand_
|
|
|
|
|
except errors.AnsibleError, e:
|
|
|
|
|
raise errors.AnsibleError("Failed to template %s: %s" % (varname, str(e)))
|
|
|
|
|
|
|
|
|
|
if (varname.startswith("{") and not varname.startswith("{{")) or varname.startswith("["):
|
|
|
|
|
eval_results = utils.safe_eval(varname, locals=templatevars, include_exceptions=True)
|
|
|
|
|
if eval_results[1] is None:
|
|
|
|
|
varname = eval_results[0]
|
|
|
|
|
# template_from_string may return non strings for the case where the var is just
|
|
|
|
|
# a reference to a single variable, so we should re_check before we do further evals
|
|
|
|
|
if isinstance(varname, basestring):
|
|
|
|
|
if (varname.startswith("{") and not varname.startswith("{{")) or varname.startswith("["):
|
|
|
|
|
eval_results = utils.safe_eval(varname, locals=templatevars, include_exceptions=True)
|
|
|
|
|
if eval_results[1] is None:
|
|
|
|
|
varname = eval_results[0]
|
|
|
|
|
|
|
|
|
|
return varname
|
|
|
|
|
|
|
|
|
@ -323,10 +331,20 @@ def template_from_file(basedir, path, vars, vault_password=None):
|
|
|
|
|
|
|
|
|
|
def template_from_string(basedir, data, vars, fail_on_undefined=False):
|
|
|
|
|
''' run a string through the (Jinja2) templating engine '''
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if type(data) == str:
|
|
|
|
|
data = unicode(data, 'utf-8')
|
|
|
|
|
|
|
|
|
|
# Check to see if the string we are trying to render is just referencing a single
|
|
|
|
|
# var. In this case we don't wont to accidentally change the type of the variable
|
|
|
|
|
# to a string by using the jinja template renderer. We just want to pass it.
|
|
|
|
|
only_one = SINGLE_VAR.match(data)
|
|
|
|
|
if only_one:
|
|
|
|
|
var_name = only_one.group(1)
|
|
|
|
|
if var_name in vars:
|
|
|
|
|
resolved_val = vars[var_name]
|
|
|
|
|
if isinstance(resolved_val, (bool, Number)):
|
|
|
|
|
return resolved_val
|
|
|
|
|
|
|
|
|
|
def my_finalize(thing):
|
|
|
|
|
return thing if thing is not None else ''
|
|
|
|
|