def __init__(self, error, value=_marker, index=None, mode=None): line_num = None buf = '' marker = ' <======================' trace = None if mode == 'exec': error, line_num, buf = self.exec_error(error, value, index) elif index: error, line_num, buf = self.index_error(error, value, index) else: error = self.basic_error(error, value) if line_num is not None: marker = ' <======[LINE {0}]======'.format(line_num) SaltRenderError.__init__(self, error, line_num, buf, marker, trace)
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context['opts'] saltenv = context['saltenv'] loader = None newline = False if tmplstr and not isinstance(tmplstr, six.text_type): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith('\n'): newline = True if not saltenv: if tmplpath: # i.e., the template is from a file outside the state tree # # XXX: FileSystemLoader is not being properly instantiated here is # it? At least it ain't according to: # # http://jinja.pocoo.org/docs/api/#jinja2.FileSystemLoader loader = jinja2.FileSystemLoader(context, os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get( '_pillar_rend', False)) env_args = {'extensions': [], 'loader': loader} if hasattr(jinja2.ext, 'with_'): env_args['extensions'].append('jinja2.ext.with_') if hasattr(jinja2.ext, 'do'): env_args['extensions'].append('jinja2.ext.do') if hasattr(jinja2.ext, 'loopcontrols'): env_args['extensions'].append('jinja2.ext.loopcontrols') env_args['extensions'].append(salt.utils.jinja.SerializerExtension) # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get('jinja_trim_blocks', False): log.debug('Jinja2 trim_blocks is enabled') env_args['trim_blocks'] = True if opts.get('jinja_lstrip_blocks', False): log.debug('Jinja2 lstrip_blocks is enabled') env_args['lstrip_blocks'] = True if opts.get('allow_undefined', False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) jinja_env.filters['strftime'] = salt.utils.date_format jinja_env.filters['sequence'] = salt.utils.jinja.ensure_sequence_filter jinja_env.filters['yaml_dquote'] = salt.utils.yamlencoding.yaml_dquote jinja_env.filters['yaml_squote'] = salt.utils.yamlencoding.yaml_squote jinja_env.filters['yaml_encode'] = salt.utils.yamlencoding.yaml_encode jinja_env.filters[ 'is_ip'] = salt.utils.network.is_ip_filter # check if valid IP address jinja_env.filters[ 'is_ipv4'] = salt.utils.network.is_ipv4_filter # check if valid IPv4 address jinja_env.filters[ 'is_ipv6'] = salt.utils.network.is_ipv6_filter # check if valid IPv6 address jinja_env.filters[ 'ipaddr'] = salt.utils.network.ipaddr # filter IP addresses jinja_env.filters[ 'ipv4'] = salt.utils.network.ipv4 # filter IPv4-only addresses jinja_env.filters[ 'ipv6'] = salt.utils.network.ipv6 # filter IPv6-only addresses jinja_env.filters[ 'ip_host'] = salt.utils.network.ip_host # return the network interface IP jinja_env.filters[ 'network_hosts'] = salt.utils.network.network_hosts # return the hosts within a network jinja_env.filters[ 'network_size'] = salt.utils.network.network_size # return the network size jinja_env.globals['odict'] = OrderedDict jinja_env.globals['show_full_context'] = salt.utils.jinja.show_full_context jinja_env.tests['list'] = salt.utils.is_list decoded_context = {} for key, value in six.iteritems(context): if not isinstance(value, string_types): decoded_context[key] = value continue decoded_context[key] = salt.utils.locales.sdecode(value) try: template = jinja_env.from_string(tmplstr) template.globals.update(decoded_context) output = template.render(**decoded_context) except jinja2.exceptions.TemplateSyntaxError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError('Jinja syntax error: {0}{1}'.format(exc, out), line, tmplstr) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=decoded_context)[1] tmplstr = '' # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 raise SaltRenderError('Jinja variable {0}{1}'.format(exc, out), buf=tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Problem running salt function in Jinja template: {0}{1}'.format( exc, out), line, tmplstr) except Exception as exc: tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' else: tmplstr += '\n{0}'.format(tracestr) log.debug("Jinja Error") log.debug("Exception: {0}".format(exc)) log.debug("Out: {0}".format(out)) log.debug("Line: {0}".format(line)) log.debug("TmplStr: {0}".format(tmplstr)) log.debug("TraceStr: {0}".format(tracestr)) raise SaltRenderError('Jinja error: {0}{1}'.format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += '\n' return output
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context['opts'] saltenv = context['saltenv'] loader = None newline = False if tmplstr and not isinstance(tmplstr, unicode): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith('\n'): newline = True if not saltenv: if tmplpath: # ie, the template is from a file outside the state tree # # XXX: FileSystemLoader is not being properly instantiated here is # it? At least it ain't according to: # # http://jinja.pocoo.org/docs/api/#jinja2.FileSystemLoader loader = jinja2.FileSystemLoader( context, os.path.dirname(tmplpath)) else: loader = JinjaSaltCacheLoader(opts, saltenv) env_args = {'extensions': [], 'loader': loader} if hasattr(jinja2.ext, 'with_'): env_args['extensions'].append('jinja2.ext.with_') if hasattr(jinja2.ext, 'do'): env_args['extensions'].append('jinja2.ext.do') if hasattr(jinja2.ext, 'loopcontrols'): env_args['extensions'].append('jinja2.ext.loopcontrols') env_args['extensions'].append(JinjaSerializerExtension) # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get('jinja_trim_blocks', False): log.debug('Jinja2 trim_blocks is enabled') env_args['trim_blocks'] = True if opts.get('jinja_lstrip_blocks', False): log.debug('Jinja2 lstrip_blocks is enabled') env_args['lstrip_blocks'] = True if opts.get('allow_undefined', False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) jinja_env.filters['strftime'] = salt.utils.date_format jinja_env.filters['sequence'] = ensure_sequence_filter jinja_env.globals['odict'] = OrderedDict jinja_env.globals['show_full_context'] = show_full_context unicode_context = {} for key, value in context.iteritems(): if not isinstance(value, string_types): unicode_context[key] = value continue # Let's try UTF-8 and fail if this still fails, that's why this is not # wrapped in a try/except if isinstance(value, unicode): unicode_context[key] = value else: unicode_context[key] = unicode(value, 'utf-8') try: template = jinja_env.from_string(tmplstr) template.globals.update(unicode_context) output = template.render(**unicode_context) except jinja2.exceptions.TemplateSyntaxError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=unicode_context) if not line: tmplstr = '' raise SaltRenderError('Jinja syntax error: {0}{1}'.format(exc, out), line, tmplstr) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=unicode_context)[1] tmplstr = '' raise SaltRenderError( 'Jinja variable {0}{1}'.format( exc, out), buf=tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=unicode_context) if not line: tmplstr = '' raise SaltRenderError( 'Problem running salt function in Jinja template: {0}{1}'.format( exc, out), line, tmplstr) except Exception as exc: tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=unicode_context) if not line: tmplstr = '' else: tmplstr += '\n{0}'.format(tracestr) raise SaltRenderError('Jinja error: {0}{1}'.format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += '\n' return output
def render(input, saltenv="base", sls="", argline="", **kws): gen_start_state = False no_goal_state = False implicit_require = False def process_sls_data(data, context=None, extract=False): sls_dir = os.path.dirname(sls.replace( ".", os.path.sep)) if "." in sls else sls ctx = dict(sls_dir=sls_dir if sls_dir else ".") if context: ctx.update(context) tmplout = render_template(StringIO(data), saltenv, sls, context=ctx, argline=rt_argline.strip(), **kws) high = render_data(tmplout, saltenv, sls, argline=rd_argline.strip()) return process_high_data(high, extract) def process_high_data(high, extract): # make a copy so that the original, un-preprocessed highstate data # structure can be used later for error checking if anything goes # wrong during the preprocessing. data = copy.deepcopy(high) try: rewrite_single_shorthand_state_decl(data) rewrite_sls_includes_excludes(data, sls, saltenv) if not extract and implicit_require: sid = has_names_decls(data) if sid: raise SaltRenderError( "'names' declaration(found in state id: {0}) is " "not supported with implicitly ordered states! You " "should generate the states in a template for-loop " "instead.".format(sid)) add_implicit_requires(data) if gen_start_state: add_start_state(data, sls) if not extract and not no_goal_state: add_goal_state(data) rename_state_ids(data, sls) # We must extract no matter what so extending a stateconf sls file # works! extract_state_confs(data) except SaltRenderError: raise except Exception as err: # pylint: disable=broad-except log.exception( "Error found while pre-processing the salt file %s:\n%s", sls, err) from salt.state import State state = State(__opts__) errors = state.verify_high(high) if errors: raise SaltRenderError("\n".join(errors)) raise SaltRenderError("sls preprocessing/rendering failed!") return data # ---------------------- renderers = kws["renderers"] opts, args = getopt.getopt(argline.split(), "Gosp") argline = " ".join(args) if args else "yaml . jinja" if ("-G", "") in opts: no_goal_state = True if ("-o", "") in opts: implicit_require = True if ("-s", "") in opts: gen_start_state = True if ("-p", "") in opts: data = process_high_data(input, extract=False) else: # Split on the first dot surrounded by spaces but not preceded by a # backslash. A backslash preceded dot will be replaced with just dot. args = [ arg.strip().replace("\\.", ".") for arg in re.split(r"\s+(?<!\\)\.\s+", argline, 1) ] try: name, rd_argline = (args[0] + " ").split(" ", 1) render_data = renderers[name] # e.g., the yaml renderer if implicit_require: if name == "yaml": rd_argline = "-o " + rd_argline else: raise SaltRenderError( "Implicit ordering is only supported if the yaml renderer " "is used!") name, rt_argline = (args[1] + " ").split(" ", 1) render_template = renderers[name] # e.g., the mako renderer except KeyError as err: raise SaltRenderError( "Renderer: {0} is not available!".format(err)) except IndexError: raise INVALID_USAGE_ERROR if isinstance(input, six.string_types): with salt.utils.files.fopen(input, "r") as ifile: sls_templ = salt.utils.stringutils.to_unicode(ifile.read()) else: # assume file-like sls_templ = salt.utils.stringutils.to_unicode(input.read()) # first pass to extract the state configuration match = re.search(__opts__["stateconf_end_marker"], sls_templ) if match: process_sls_data(sls_templ[:match.start()], extract=True) # if some config has been extracted then remove the sls-name prefix # of the keys in the extracted stateconf.set context to make them easier # to use in the salt file. if STATE_CONF: tmplctx = STATE_CONF.copy() if tmplctx: prefix = sls + "::" for k in six.iterkeys(tmplctx): # iterate over a copy of keys if k.startswith(prefix): tmplctx[k[len(prefix):]] = tmplctx[k] del tmplctx[k] else: tmplctx = {} # do a second pass that provides the extracted conf as template context data = process_sls_data(sls_templ, tmplctx) if log.isEnabledFor(logging.DEBUG): import pprint # FIXME: pprint OrderedDict log.debug("Rendered sls: %s", pprint.pformat(data)) return data
STATE_NAME = STATE_FUNC.split(".")[0] MOD_BASENAME = os.path.basename(__file__) INVALID_USAGE_ERROR = SaltRenderError( "Invalid use of {0} renderer!\n" """Usage: #!{0} [-GoSp] [<data_renderer> [options] . <template_renderer> [options]] where an example <data_renderer> would be yaml and a <template_renderer> might be jinja. Each renderer can be passed its renderer specific options. Options(for this renderer): -G Do not generate the goal state that requires all other states in the sls. -o Indirectly order the states by adding requires such that they will be executed in the order they are defined in the sls. Implies using yaml -o. -s Generate the start state that gets inserted as the first state in the sls. This only makes sense if your high state data dict is ordered. -p Assume high state input. This option allows you to pipe high state data through this renderer. With this option, the use of stateconf.set state in the sls will have no effect, but other features of the renderer still apply. """.format(MOD_BASENAME)) def render(input, saltenv="base", sls="", argline="", **kws): gen_start_state = False
def render(input, saltenv='base', sls='', argline='', **kws): gen_start_state = False no_goal_state = False implicit_require = False def process_sls_data(data, context=None, extract=False): sls_dir = ospath.dirname(sls.replace('.', ospath.sep)) if '.' in sls else sls ctx = dict(sls_dir=sls_dir if sls_dir else '.') if context: ctx.update(context) tmplout = render_template( StringIO(data), saltenv, sls, context=ctx, argline=rt_argline.strip(), **kws ) high = render_data(tmplout, saltenv, sls, argline=rd_argline.strip()) return process_high_data(high, extract) def process_high_data(high, extract): # make a copy so that the original, un-preprocessed highstate data # structure can be used later for error checking if anything goes # wrong during the preprocessing. data = copy.deepcopy(high) try: rewrite_single_shorthand_state_decl(data) rewrite_sls_includes_excludes(data, sls, saltenv) if not extract and implicit_require: sid = has_names_decls(data) if sid: raise SaltRenderError( '\'names\' declaration(found in state id: {0}) is ' 'not supported with implicitly ordered states! You ' 'should generate the states in a template for-loop ' 'instead.'.format(sid) ) add_implicit_requires(data) if gen_start_state: add_start_state(data, sls) if not extract and not no_goal_state: add_goal_state(data) rename_state_ids(data, sls) # We must extract no matter what so extending a stateconf sls file # works! extract_state_confs(data) except SaltRenderError: raise except Exception as err: log.exception( 'Error found while pre-processing the salt file ' '{0}:\n{1}'.format(sls, err) ) from salt.state import State state = State(__opts__) errors = state.verify_high(high) if errors: raise SaltRenderError('\n'.join(errors)) raise SaltRenderError('sls preprocessing/rendering failed!') return data # ---------------------- renderers = kws['renderers'] opts, args = getopt.getopt(argline.split(), 'Gosp') argline = ' '.join(args) if args else 'yaml . jinja' if ('-G', '') in opts: no_goal_state = True if ('-o', '') in opts: implicit_require = True if ('-s', '') in opts: gen_start_state = True if ('-p', '') in opts: data = process_high_data(input, extract=False) else: # Split on the first dot surrounded by spaces but not preceded by a # backslash. A backslash preceded dot will be replaced with just dot. args = [ arg.strip().replace('\\.', '.') for arg in re.split(r'\s+(?<!\\)\.\s+', argline, 1) ] try: name, rd_argline = (args[0] + ' ').split(' ', 1) render_data = renderers[name] # e.g., the yaml renderer if implicit_require: if name == 'yaml': rd_argline = '-o ' + rd_argline else: raise SaltRenderError( 'Implicit ordering is only supported if the yaml renderer ' 'is used!' ) name, rt_argline = (args[1] + ' ').split(' ', 1) render_template = renderers[name] # e.g., the mako renderer except KeyError as err: raise SaltRenderError('Renderer: {0} is not available!'.format(err)) except IndexError: raise INVALID_USAGE_ERROR if isinstance(input, six.string_types): with salt.utils.fopen(input, 'r') as ifile: sls_templ = ifile.read() else: # assume file-like sls_templ = input.read() # first pass to extract the state configuration match = re.search(__opts__['stateconf_end_marker'], sls_templ) if match: process_sls_data(sls_templ[:match.start()], extract=True) # if some config has been extracted then remove the sls-name prefix # of the keys in the extracted stateconf.set context to make them easier # to use in the salt file. if STATE_CONF: tmplctx = STATE_CONF.copy() if tmplctx: prefix = sls + '::' for k in six.iterkeys(tmplctx): # iterate over a copy of keys if k.startswith(prefix): tmplctx[k[len(prefix):]] = tmplctx[k] del tmplctx[k] else: tmplctx = {} # do a second pass that provides the extracted conf as template context data = process_sls_data(sls_templ, tmplctx) if log.isEnabledFor(logging.DEBUG): import pprint # FIXME: pprint OrderedDict log.debug('Rendered sls: {0}'.format(pprint.pformat(data))) return data
def _sls_method(*args, **kws): sls = pydsl.Sls.get_render_stack()[-1] try: return getattr(sls, method.__name__)(*args, **kws) except PyDslError as exc: raise SaltRenderError(exc)
def render_jinja_tmpl(tmplstr, context, tmplpath=None): """ Render a Jinja template. :param str tmplstr: A string containing the source to be rendered. :param dict context: Any additional context data used by the renderer. :param str tmplpath: Base path from which ``tmplstr`` may load additional template files. :returns str: The string rendered by the template. """ opts = context["opts"] saltenv = context["saltenv"] loader = None newline = False file_client = context.get("fileclient", None) if tmplstr and not isinstance(tmplstr, str): # https://jinja.palletsprojects.com/en/2.11.x/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith(os.linesep): newline = os.linesep elif tmplstr.endswith("\n"): newline = "\n" if not saltenv: if tmplpath: loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader( opts, saltenv, pillar_rend=context.get("_pillar_rend", False), _file_client=file_client, ) env_args = {"extensions": [], "loader": loader} if hasattr(jinja2.ext, "with_"): env_args["extensions"].append("jinja2.ext.with_") if hasattr(jinja2.ext, "do"): env_args["extensions"].append("jinja2.ext.do") if hasattr(jinja2.ext, "loopcontrols"): env_args["extensions"].append("jinja2.ext.loopcontrols") env_args["extensions"].append(salt.utils.jinja.SerializerExtension) opt_jinja_env = opts.get("jinja_env", {}) opt_jinja_sls_env = opts.get("jinja_sls_env", {}) opt_jinja_env = opt_jinja_env if isinstance(opt_jinja_env, dict) else {} opt_jinja_sls_env = opt_jinja_sls_env if isinstance( opt_jinja_sls_env, dict) else {} # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get("jinja_trim_blocks", False): log.debug("Jinja2 trim_blocks is enabled") log.warning( "jinja_trim_blocks is deprecated and will be removed in a future release," " please use jinja_env and/or jinja_sls_env instead") opt_jinja_env["trim_blocks"] = True opt_jinja_sls_env["trim_blocks"] = True if opts.get("jinja_lstrip_blocks", False): log.debug("Jinja2 lstrip_blocks is enabled") log.warning( "jinja_lstrip_blocks is deprecated and will be removed in a future release," " please use jinja_env and/or jinja_sls_env instead") opt_jinja_env["lstrip_blocks"] = True opt_jinja_sls_env["lstrip_blocks"] = True def opt_jinja_env_helper(opts, optname): for k, v in opts.items(): k = k.lower() if hasattr(jinja2.defaults, k.upper()): log.debug("Jinja2 environment %s was set to %s by %s", k, v, optname) env_args[k] = v else: log.warning("Jinja2 environment %s is not recognized", k) if "sls" in context and context["sls"] != "": opt_jinja_env_helper(opt_jinja_sls_env, "jinja_sls_env") else: opt_jinja_env_helper(opt_jinja_env, "jinja_env") if opts.get("allow_undefined", False): jinja_env = jinja2.sandbox.SandboxedEnvironment(**env_args) else: jinja_env = jinja2.sandbox.SandboxedEnvironment( undefined=jinja2.StrictUndefined, **env_args) indent_filter = jinja_env.filters.get("indent") jinja_env.tests.update(JinjaTest.salt_jinja_tests) jinja_env.filters.update(JinjaFilter.salt_jinja_filters) if salt.utils.jinja.JINJA_VERSION >= LooseVersion("2.11"): # Use the existing indent filter on Jinja versions where it's not broken jinja_env.filters["indent"] = indent_filter jinja_env.globals.update(JinjaGlobal.salt_jinja_globals) # globals jinja_env.globals["odict"] = OrderedDict jinja_env.globals["show_full_context"] = salt.utils.jinja.show_full_context jinja_env.tests["list"] = salt.utils.data.is_list decoded_context = {} for key, value in context.items(): if not isinstance(value, str): if isinstance(value, NamedLoaderContext): decoded_context[key] = value.value() else: decoded_context[key] = value continue try: decoded_context[key] = salt.utils.stringutils.to_unicode( value, encoding=SLS_ENCODING) except UnicodeDecodeError as ex: log.debug( "Failed to decode using default encoding (%s), trying system encoding", SLS_ENCODING, ) decoded_context[key] = salt.utils.data.decode(value) jinja_env.globals.update(decoded_context) try: template = jinja_env.from_string(tmplstr) output = template.render(**decoded_context) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" raise SaltRenderError("Jinja variable {}{}".format(exc, out), line, tmplstr) except ( jinja2.exceptions.TemplateRuntimeError, jinja2.exceptions.TemplateSyntaxError, jinja2.exceptions.SecurityError, ) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" raise SaltRenderError("Jinja syntax error: {}{}".format(exc, out), line, tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" raise SaltRenderError( "Problem running salt function in Jinja template: {}{}".format( exc, out), line, tmplstr, ) except Exception as exc: # pylint: disable=broad-except tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" else: tmplstr += "\n{}".format(tracestr) log.debug("Jinja Error") log.debug("Exception:", exc_info=True) log.debug("Out: %s", out) log.debug("Line: %s", line) log.debug("TmplStr: %s", tmplstr) log.debug("TraceStr: %s", tracestr) raise SaltRenderError("Jinja error: {}{}".format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += newline return output
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context['opts'] saltenv = context['saltenv'] loader = None newline = False if tmplstr and not isinstance(tmplstr, six.text_type): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith('\n'): newline = True if not saltenv: if tmplpath: loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get('_pillar_rend', False)) env_args = {'extensions': [], 'loader': loader} if hasattr(jinja2.ext, 'with_'): env_args['extensions'].append('jinja2.ext.with_') if hasattr(jinja2.ext, 'do'): env_args['extensions'].append('jinja2.ext.do') if hasattr(jinja2.ext, 'loopcontrols'): env_args['extensions'].append('jinja2.ext.loopcontrols') env_args['extensions'].append(salt.utils.jinja.SerializerExtension) # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get('jinja_trim_blocks', False): log.debug('Jinja2 trim_blocks is enabled') env_args['trim_blocks'] = True if opts.get('jinja_lstrip_blocks', False): log.debug('Jinja2 lstrip_blocks is enabled') env_args['lstrip_blocks'] = True if opts.get('allow_undefined', False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) jinja_env.tests.update(JinjaTest.salt_jinja_tests) jinja_env.filters.update(JinjaFilter.salt_jinja_filters) jinja_env.globals.update(JinjaGlobal.salt_jinja_globals) # globals jinja_env.globals['odict'] = OrderedDict jinja_env.globals['show_full_context'] = salt.utils.jinja.show_full_context jinja_env.tests['list'] = salt.utils.is_list decoded_context = {} for key, value in six.iteritems(context): if not isinstance(value, six.string_types): decoded_context[key] = value continue decoded_context[key] = salt.utils.locales.sdecode(value) try: template = jinja_env.from_string(tmplstr) template.globals.update(decoded_context) output = template.render(**decoded_context) except jinja2.exceptions.TemplateSyntaxError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Jinja syntax error: {0}{1}'.format(exc, out), line, tmplstr) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=decoded_context)[1] tmplstr = '' # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 raise SaltRenderError( 'Jinja variable {0}{1}'.format( exc, out), buf=tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Problem running salt function in Jinja template: {0}{1}'.format( exc, out), line, tmplstr) except Exception as exc: tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' else: tmplstr += '\n{0}'.format(tracestr) log.debug("Jinja Error") log.debug("Exception: {0}".format(exc)) log.debug("Out: {0}".format(out)) log.debug("Line: {0}".format(line)) log.debug("TmplStr: {0}".format(tmplstr)) log.debug("TraceStr: {0}".format(tracestr)) raise SaltRenderError('Jinja error: {0}{1}'.format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += '\n' return output
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context['opts'] saltenv = context['saltenv'] loader = None newline = False if tmplstr and not isinstance(tmplstr, six.text_type): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith(os.linesep): newline = True if not saltenv: if tmplpath: loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get('_pillar_rend', False)) env_args = {'extensions': [], 'loader': loader} if hasattr(jinja2.ext, 'with_'): env_args['extensions'].append('jinja2.ext.with_') if hasattr(jinja2.ext, 'do'): env_args['extensions'].append('jinja2.ext.do') if hasattr(jinja2.ext, 'loopcontrols'): env_args['extensions'].append('jinja2.ext.loopcontrols') env_args['extensions'].append(salt.utils.jinja.SerializerExtension) opt_jinja_env = opts.get('jinja_env', {}) opt_jinja_sls_env = opts.get('jinja_sls_env', {}) opt_jinja_env = opt_jinja_env if isinstance(opt_jinja_env, dict) else {} opt_jinja_sls_env = opt_jinja_sls_env if isinstance(opt_jinja_sls_env, dict) else {} # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get('jinja_trim_blocks', False): log.debug('Jinja2 trim_blocks is enabled') log.warning('jinja_trim_blocks is deprecated and will be removed in a future release, please use jinja_env and/or jinja_sls_env instead') opt_jinja_env['trim_blocks'] = True opt_jinja_sls_env['trim_blocks'] = True if opts.get('jinja_lstrip_blocks', False): log.debug('Jinja2 lstrip_blocks is enabled') log.warning('jinja_lstrip_blocks is deprecated and will be removed in a future release, please use jinja_env and/or jinja_sls_env instead') opt_jinja_env['lstrip_blocks'] = True opt_jinja_sls_env['lstrip_blocks'] = True def opt_jinja_env_helper(opts, optname): for k, v in six.iteritems(opts): k = k.lower() if hasattr(jinja2.defaults, k.upper()): log.debug('Jinja2 environment %s was set to %s by %s', k, v, optname) env_args[k] = v else: log.warning('Jinja2 environment %s is not recognized', k) if 'sls' in context and context['sls'] != '': opt_jinja_env_helper(opt_jinja_sls_env, 'jinja_sls_env') else: opt_jinja_env_helper(opt_jinja_env, 'jinja_env') if opts.get('allow_undefined', False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) jinja_env.tests.update(JinjaTest.salt_jinja_tests) jinja_env.filters.update(JinjaFilter.salt_jinja_filters) jinja_env.globals.update(JinjaGlobal.salt_jinja_globals) # globals jinja_env.globals['odict'] = OrderedDict jinja_env.globals['show_full_context'] = salt.utils.jinja.show_full_context jinja_env.tests['list'] = salt.utils.data.is_list decoded_context = {} for key, value in six.iteritems(context): if not isinstance(value, six.string_types): decoded_context[key] = value continue try: decoded_context[key] = salt.utils.stringutils.to_unicode(value, encoding=SLS_ENCODING) except UnicodeDecodeError as ex: log.debug( "Failed to decode using default encoding (%s), trying system encoding", SLS_ENCODING, ) decoded_context[key] = salt.utils.data.decode(value) try: template = jinja_env.from_string(tmplstr) template.globals.update(decoded_context) output = template.render(**decoded_context) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=decoded_context)[1] tmplstr = '' # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 raise SaltRenderError( 'Jinja variable {0}{1}'.format( exc, out), buf=tmplstr) except (jinja2.exceptions.TemplateRuntimeError, jinja2.exceptions.TemplateSyntaxError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Jinja syntax error: {0}{1}'.format(exc, out), line, tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Problem running salt function in Jinja template: {0}{1}'.format( exc, out), line, tmplstr) except Exception as exc: tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' else: tmplstr += '\n{0}'.format(tracestr) log.debug('Jinja Error') log.debug('Exception:', exc_info=True) log.debug('Out: %s', out) log.debug('Line: %s', line) log.debug('TmplStr: %s', tmplstr) log.debug('TraceStr: %s', tracestr) raise SaltRenderError('Jinja error: {0}{1}'.format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += os.linesep return output
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context["opts"] saltenv = context["saltenv"] loader = None newline = False if tmplstr and not isinstance(tmplstr, six.text_type): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith(os.linesep): newline = os.linesep elif tmplstr.endswith("\n"): newline = "\n" if not saltenv: if tmplpath: loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get( "_pillar_rend", False)) env_args = {"extensions": [], "loader": loader} if hasattr(jinja2.ext, "with_"): env_args["extensions"].append("jinja2.ext.with_") if hasattr(jinja2.ext, "do"): env_args["extensions"].append("jinja2.ext.do") if hasattr(jinja2.ext, "loopcontrols"): env_args["extensions"].append("jinja2.ext.loopcontrols") env_args["extensions"].append(salt.utils.jinja.SerializerExtension) opt_jinja_env = opts.get("jinja_env", {}) opt_jinja_sls_env = opts.get("jinja_sls_env", {}) opt_jinja_env = opt_jinja_env if isinstance(opt_jinja_env, dict) else {} opt_jinja_sls_env = opt_jinja_sls_env if isinstance( opt_jinja_sls_env, dict) else {} # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get("jinja_trim_blocks", False): log.debug("Jinja2 trim_blocks is enabled") log.warning( "jinja_trim_blocks is deprecated and will be removed in a future release, please use jinja_env and/or jinja_sls_env instead" ) opt_jinja_env["trim_blocks"] = True opt_jinja_sls_env["trim_blocks"] = True if opts.get("jinja_lstrip_blocks", False): log.debug("Jinja2 lstrip_blocks is enabled") log.warning( "jinja_lstrip_blocks is deprecated and will be removed in a future release, please use jinja_env and/or jinja_sls_env instead" ) opt_jinja_env["lstrip_blocks"] = True opt_jinja_sls_env["lstrip_blocks"] = True def opt_jinja_env_helper(opts, optname): for k, v in six.iteritems(opts): k = k.lower() if hasattr(jinja2.defaults, k.upper()): log.debug("Jinja2 environment %s was set to %s by %s", k, v, optname) env_args[k] = v else: log.warning("Jinja2 environment %s is not recognized", k) if "sls" in context and context["sls"] != "": opt_jinja_env_helper(opt_jinja_sls_env, "jinja_sls_env") else: opt_jinja_env_helper(opt_jinja_env, "jinja_env") if opts.get("allow_undefined", False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) tojson_filter = jinja_env.filters.get("tojson") jinja_env.tests.update(JinjaTest.salt_jinja_tests) jinja_env.filters.update(JinjaFilter.salt_jinja_filters) if tojson_filter is not None: # Use the existing tojson filter, if present (jinja2 >= 2.9) jinja_env.filters["tojson"] = tojson_filter jinja_env.globals.update(JinjaGlobal.salt_jinja_globals) # globals jinja_env.globals["odict"] = OrderedDict jinja_env.globals["show_full_context"] = salt.utils.jinja.show_full_context jinja_env.tests["list"] = salt.utils.data.is_list decoded_context = {} for key, value in six.iteritems(context): if not isinstance(value, six.string_types): decoded_context[key] = value continue try: decoded_context[key] = salt.utils.stringutils.to_unicode( value, encoding=SLS_ENCODING) except UnicodeDecodeError as ex: log.debug( "Failed to decode using default encoding (%s), trying system encoding", SLS_ENCODING, ) decoded_context[key] = salt.utils.data.decode(value) try: template = jinja_env.from_string(tmplstr) template.globals.update(decoded_context) output = template.render(**decoded_context) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=decoded_context)[1] tmplstr = "" # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 raise SaltRenderError("Jinja variable {0}{1}".format(exc, out), buf=tmplstr) except ( jinja2.exceptions.TemplateRuntimeError, jinja2.exceptions.TemplateSyntaxError, ) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" raise SaltRenderError("Jinja syntax error: {0}{1}".format(exc, out), line, tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" raise SaltRenderError( "Problem running salt function in Jinja template: {0}{1}".format( exc, out), line, tmplstr, ) except Exception as exc: # pylint: disable=broad-except tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = "" else: tmplstr += "\n{0}".format(tracestr) log.debug("Jinja Error") log.debug("Exception:", exc_info=True) log.debug("Out: %s", out) log.debug("Line: %s", line) log.debug("TmplStr: %s", tmplstr) log.debug("TraceStr: %s", tracestr) raise SaltRenderError("Jinja error: {0}{1}".format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += newline return output
.. code-block: - xml - xhtml - html - text - newtext - oldtext Note that the ``text`` method will call ``NewTextTemplate``. If ``oldtext`` is desired, it must be called explicitly :rtype: A Python data structure ''' if not HAS_LIBS: raise SaltRenderError('Failed to import genshi template') if not isinstance(genshi_data, six.string_types): genshi_data = genshi_data.read() if genshi_data.startswith('#!'): genshi_data = genshi_data[(genshi_data.find('\n') + 1):] if not genshi_data.strip(): return {} if method == 'text' or method == 'newtext': tmpl = NewTextTemplate(genshi_data) elif method == 'oldtext': tmpl = OldTextTemplate(genshi_data) else: tmpl = MarkupTemplate(genshi_data)
def render_jinja_tmpl(tmplstr, context, tmplpath=None): opts = context['opts'] saltenv = context['saltenv'] loader = None newline = False if tmplstr and not isinstance(tmplstr, six.text_type): # http://jinja.pocoo.org/docs/api/#unicode tmplstr = tmplstr.decode(SLS_ENCODING) if tmplstr.endswith(os.linesep): newline = True if not saltenv: if tmplpath: loader = jinja2.FileSystemLoader(os.path.dirname(tmplpath)) else: loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get( '_pillar_rend', False)) env_args = {'extensions': [], 'loader': loader} if hasattr(jinja2.ext, 'with_'): env_args['extensions'].append('jinja2.ext.with_') if hasattr(jinja2.ext, 'do'): env_args['extensions'].append('jinja2.ext.do') if hasattr(jinja2.ext, 'loopcontrols'): env_args['extensions'].append('jinja2.ext.loopcontrols') env_args['extensions'].append(salt.utils.jinja.SerializerExtension) # Pass through trim_blocks and lstrip_blocks Jinja parameters # trim_blocks removes newlines around Jinja blocks # lstrip_blocks strips tabs and spaces from the beginning of # line to the start of a block. if opts.get('jinja_trim_blocks', False): log.debug('Jinja2 trim_blocks is enabled') env_args['trim_blocks'] = True if opts.get('jinja_lstrip_blocks', False): log.debug('Jinja2 lstrip_blocks is enabled') env_args['lstrip_blocks'] = True if opts.get('allow_undefined', False): jinja_env = jinja2.Environment(**env_args) else: jinja_env = jinja2.Environment(undefined=jinja2.StrictUndefined, **env_args) jinja_env.filters['strftime'] = salt.utils.date_format jinja_env.filters['sequence'] = salt.utils.jinja.ensure_sequence_filter jinja_env.filters['http_query'] = salt.utils.http.query jinja_env.filters['to_bool'] = salt.utils.jinja.to_bool jinja_env.filters['exactly_n_true'] = salt.utils.exactly_n jinja_env.filters['exactly_one_true'] = salt.utils.exactly_one jinja_env.filters['quote'] = salt.utils.jinja.quote jinja_env.filters['regex_search'] = salt.utils.jinja.regex_search jinja_env.filters['regex_match'] = salt.utils.jinja.regex_match jinja_env.filters['regex_replace'] = salt.utils.jinja.regex_replace jinja_env.filters['uuid'] = salt.utils.jinja.uuid_ jinja_env.filters['min'] = salt.utils.jinja.lst_min jinja_env.filters['max'] = salt.utils.jinja.lst_max jinja_env.filters['avg'] = salt.utils.jinja.lst_avg jinja_env.filters['union'] = salt.utils.jinja.union jinja_env.filters['intersect'] = salt.utils.jinja.intersect jinja_env.filters['difference'] = salt.utils.jinja.difference jinja_env.filters[ 'symmetric_difference'] = salt.utils.jinja.symmetric_difference jinja_env.filters['md5'] = salt.utils.hashutils.md5_digest jinja_env.filters['sha256'] = salt.utils.hashutils.sha256_digest jinja_env.filters['sha512'] = salt.utils.hashutils.sha512_digest jinja_env.filters['hmac'] = salt.utils.hashutils.hmac_signature jinja_env.filters['is_sorted'] = salt.utils.isorted jinja_env.filters['is_text_file'] = salt.utils.istextfile jinja_env.filters['is_empty_file'] = salt.utils.is_empty jinja_env.filters['is_binary_file'] = salt.utils.is_bin_file jinja_env.filters['file_hashsum'] = salt.utils.get_hash jinja_env.filters['is_hex'] = salt.utils.is_hex jinja_env.filters['path_join'] = salt.utils.path_join jinja_env.filters['dns_check'] = salt.utils.dns_check jinja_env.filters['list_files'] = salt.utils.list_files jinja_env.filters['which'] = salt.utils.which jinja_env.filters['random_str'] = salt.utils.rand_str jinja_env.filters['get_uid'] = salt.utils.get_uid jinja_env.filters['mysql_to_dict'] = salt.utils.mysql_to_dict jinja_env.filters['contains_whitespace'] = salt.utils.contains_whitespace jinja_env.filters['str_to_num'] = salt.utils.str_to_num jinja_env.filters[ 'check_whitelist_blacklist'] = salt.utils.check_whitelist_blacklist jinja_env.filters['mac_str_to_bytes'] = salt.utils.mac_str_to_bytes jinja_env.filters['date_format'] = salt.utils.date_format jinja_env.filters['compare_dicts'] = salt.utils.compare_dicts jinja_env.filters['compare_lists'] = salt.utils.compare_lists jinja_env.filters['json_decode_list'] = salt.utils.decode_list jinja_env.filters['json_decode_dict'] = salt.utils.decode_dict jinja_env.filters['is_list'] = salt.utils.is_list jinja_env.filters['is_iter'] = salt.utils.is_iter jinja_env.filters['to_bytes'] = salt.utils.to_bytes jinja_env.filters['substring_in_list'] = salt.utils.substr_in_list jinja_env.filters['base64_encode'] = salt.utils.hashutils.base64_b64encode jinja_env.filters['base64_decode'] = salt.utils.hashutils.base64_b64decode jinja_env.filters['yaml_dquote'] = salt.utils.yamlencoding.yaml_dquote jinja_env.filters['yaml_squote'] = salt.utils.yamlencoding.yaml_squote jinja_env.filters['yaml_encode'] = salt.utils.yamlencoding.yaml_encode jinja_env.filters['gen_mac'] = salt.utils.gen_mac jinja_env.filters[ 'is_ip'] = salt.utils.network.is_ip_filter # check if valid IP address jinja_env.filters[ 'is_ipv4'] = salt.utils.network.is_ipv4_filter # check if valid IPv4 address jinja_env.filters[ 'is_ipv6'] = salt.utils.network.is_ipv6_filter # check if valid IPv6 address jinja_env.filters[ 'ipaddr'] = salt.utils.network.ipaddr # filter IP addresses jinja_env.filters[ 'ipv4'] = salt.utils.network.ipv4 # filter IPv4-only addresses jinja_env.filters[ 'ipv6'] = salt.utils.network.ipv6 # filter IPv6-only addresses jinja_env.filters[ 'ip_host'] = salt.utils.network.ip_host # return the network interface IP jinja_env.filters[ 'network_hosts'] = salt.utils.network.network_hosts # return the hosts within a network jinja_env.filters[ 'network_size'] = salt.utils.network.network_size # return the network size # globals jinja_env.globals['odict'] = OrderedDict jinja_env.globals['show_full_context'] = salt.utils.jinja.show_full_context jinja_env.tests['list'] = salt.utils.is_list decoded_context = {} for key, value in six.iteritems(context): if not isinstance(value, six.string_types): decoded_context[key] = value continue try: decoded_context[key] = salt.utils.to_unicode(value, encoding=SLS_ENCODING) except UnicodeDecodeError as ex: log.debug( "Failed to decode using default encoding (%s), trying system encoding", SLS_ENCODING, ) decoded_context[key] = salt.utils.locales.sdecode(value) try: template = jinja_env.from_string(tmplstr) template.globals.update(decoded_context) output = template.render(**decoded_context) except jinja2.exceptions.TemplateSyntaxError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError('Jinja syntax error: {0}{1}'.format(exc, out), line, tmplstr) except jinja2.exceptions.UndefinedError as exc: trace = traceback.extract_tb(sys.exc_info()[2]) out = _get_jinja_error(trace, context=decoded_context)[1] tmplstr = '' # Don't include the line number, since it is misreported # https://github.com/mitsuhiko/jinja2/issues/276 raise SaltRenderError('Jinja variable {0}{1}'.format(exc, out), buf=tmplstr) except (SaltInvocationError, CommandExecutionError) as exc: trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' raise SaltRenderError( 'Problem running salt function in Jinja template: {0}{1}'.format( exc, out), line, tmplstr) except Exception as exc: tracestr = traceback.format_exc() trace = traceback.extract_tb(sys.exc_info()[2]) line, out = _get_jinja_error(trace, context=decoded_context) if not line: tmplstr = '' else: tmplstr += '\n{0}'.format(tracestr) log.debug("Jinja Error") log.debug("Exception: {0}".format(exc)) log.debug("Out: {0}".format(out)) log.debug("Line: {0}".format(line)) log.debug("TmplStr: {0}".format(tmplstr)) log.debug("TraceStr: {0}".format(tracestr)) raise SaltRenderError('Jinja error: {0}{1}'.format(exc, out), line, tmplstr, trace=tracestr) # Workaround a bug in Jinja that removes the final newline # (https://github.com/mitsuhiko/jinja2/issues/75) if newline: output += os.linesep return output