diff --git a/salt/utils/jinja.py b/salt/utils/jinja.py index eb2f4cb8de45..f0069f6fb3cb 100644 --- a/salt/utils/jinja.py +++ b/salt/utils/jinja.py @@ -1012,6 +1012,8 @@ def explore(data): @_handle_strict_undefined def format_json(self, value, sort_keys=True, indent=None): + if isinstance(value, jinja2.StrictUndefined): + return value json_txt = salt.utils.json.dumps( value, sort_keys=sort_keys, indent=indent ).strip() @@ -1020,8 +1022,9 @@ def format_json(self, value, sort_keys=True, indent=None): except UnicodeDecodeError: return Markup(salt.utils.stringutils.to_unicode(json_txt)) - @_handle_strict_undefined def format_yaml(self, value, flow_style=True): + if isinstance(value, jinja2.StrictUndefined): + return value yaml_txt = salt.utils.yaml.safe_dump( value, default_flow_style=flow_style ).strip() @@ -1041,6 +1044,8 @@ def format_xml(self, value): :returns: Formatted XML string rendered with newlines and indentation :rtype: str """ + if isinstance(value, jinja2.StrictUndefined): + return value def normalize_iter(value): if isinstance(value, (list, tuple)): @@ -1089,12 +1094,14 @@ def recurse_tree(xmliter, element=None): ).toprettyxml(indent=" ") ) - @_handle_strict_undefined def format_python(self, value): + if isinstance(value, jinja2.StrictUndefined): + return value return Markup(pprint.pformat(value).strip()) - @_handle_strict_undefined def load_yaml(self, value): + if isinstance(value, jinja2.StrictUndefined): + return value if isinstance(value, TemplateModule): value = str(value) try: @@ -1119,8 +1126,9 @@ def load_yaml(self, value): except AttributeError: raise TemplateRuntimeError(f"Unable to load yaml from {value}") - @_handle_strict_undefined def load_json(self, value): + if isinstance(value, jinja2.StrictUndefined): + return value if isinstance(value, TemplateModule): value = str(value) try: @@ -1128,8 +1136,9 @@ def load_json(self, value): except (ValueError, TypeError, AttributeError): raise TemplateRuntimeError(f"Unable to load json from {value}") - @_handle_strict_undefined def load_text(self, value): + if isinstance(value, jinja2.StrictUndefined): + return value if isinstance(value, TemplateModule): value = str(value) @@ -1255,7 +1264,6 @@ def parse_import(self, parser, converter): parser, import_node.template, f"import_{converter}", body, lineno ) - @_handle_strict_undefined def dict_to_sls_yaml_params(self, value, flow_style=False): """ .. versionadded:: 3005 @@ -1272,6 +1280,8 @@ def dict_to_sls_yaml_params(self, value, flow_style=False): :returns: Formatted SLS YAML string rendered with newlines and indentation """ + if isinstance(value, jinja2.StrictUndefined): + return value return self.format_yaml( [{key: val} for key, val in value.items()], flow_style=flow_style )