DEV Community

Dinesh Chavan
Dinesh Chavan

Posted on

1

Escaping jinja2

Hello Experts,
I want to escape '\' in jinja template am using in terraform, I tried multiple ways but somehow it's not happening!
Below is the code snippet :

{%- if mrecords['type'] == "TXT" %}
                        {%- if mrecords['values'] is defined -%}
                            {%- set txtrecord_set = namespace(value=[]) -%}
                            {%- for txtrecord in mrecords['values'] -%}
{%- set txt_value = '"' ~ '\' ~ '"' ~ txtrecord ~ '\' ~ '"' ~ '"'  -%}                              
                                {%- set txtrecord_set.value = txtrecord_set.value | append(txt_value) -%}
                            {%- endfor -%}

Enter fullscreen mode Exit fullscreen mode

Error:

Unable to parse statement "if": Unable to parse statement "for": Unable to parse statement "set": Malformed 'set'-tag args. (Line: 63 Col: 66, near "' ~ txtrecord ~ '' ~ '")
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
mxglt profile image
Maxime Guilbert

Hi
Did you try '\' ?
Not sure if it will work but in a lot of cases, you need to double backslash to escape backslash

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more