Escaping Quotes in Jinja2

In Ansible Jinja template, How to escape single quote which is inside the double quote?

Quoting my favorite resource Learn yaml in Y minutes

#... (in yaml)
single quotes: 'have ''one'' escape pattern'
double quotes: "have many: \", \0, \t, \u263A, \x0d\x0a == \r\n, and >more."

Since your outer quoting is single, you should write your value like this:

option: '"xyz ''ZZZ'' abc.de *.abc.de"'

Off topic extra answer: your condition in your template seems rather strange. You can simply check if your value is true and add extra security with the bool filter:

{% if item.enabled | bool %}

Escaping double quotes while rendering in Jinja2

How can I escape single quotes for Ansible/Jinja2 ternary operator?

This is not a quoting problem, this is an operator precedence problem.

In your example, you:

  1. apply the ternary filter to the empty string ''
  2. compare the above result to date_start

What you need to do is to enclose the condition in parens:

date_param: "{{ (date_start != '') | ternary('--date=\''+date_start+'\'', '') }}"

How to handle quotes in quotes in quotes with jinja/html?

One approach is to store the result of url_for in a variable and use it instead of calling url_for.

{% set url = url_for("admin.pairs(entry.id)") %}

<button onclick="window.location.href='{{ url }}';">PP</button>

Jinja2 NativeTemplate rendering strings with double quotes issue

I've raised the issue to Jinja, and got the following answer:
https://github.com/pallets/jinja/issues/1518#issuecomment-950326763

"This is due to the way escapes work when parsing Python strings. You need to double up the \\"

Indeed, doubling the backslashes did the trick:

path = '"G:\\\\Shared drives\\\\2 test\\\\2021\\\\08.2021\\\\test.xlsx"'
print(literal_eval(path))

# output:
> G:\Shared drives\2 test\2021\08.2021\test.xlsx

And if I want to keep the double quotes:

path = '"\\"G:\\\\Shared drives\\\\2 test\\\\2021\\\\08.2021\\\\test.xlsx\\""'
print(literal_eval(path))

# output
> "G:\Shared drives\2 test\2021\08.2021\test.xlsx"

Remove outside double quotes from list of jinja strings

You're creating a string, which needs to be delimited somehow, and will always show outer quotes when printed/logged.

If you're templating those values into another file or string, then you'll already have what you want.

{% for r in ref_format_results %}
select * from {{ "{{" ~ r ~ "}}" }};
{% endfor %}

will produce:

select * from {{ ref('model_a') }};
select * from {{ ref('model_b') }};
select * from {{ ref('model_c') }};

But more importantly, why set result to a string in the first place? Are you trying to delay the execution of the ref() macro? If not, this will work just fine, to give you a list of evaluated refs (which are Relations):

{% for result in results_list %}
{% set result = ref(result) %}
{{ ref_format_results.append(result) }}
{% endfor %}

{% do log(ref_format_results, info=true) %}

Outputs:

['"my_db"."my_schema"."model_a"', '"my_db"."my_schema"."model_b"', '"my_db"."my_schema"."model_c"']

Ansible and jinja - Sending double-quote escaped JSON string into AWS

It's a little long but this should get you what you need.

playbook

---
- hosts: localhost

tasks:
- name: get string
shell: "python3.7 print-string.py"
register: json_string
- name: Update secret in AWS
aws_secret:
name: 'testing/json_string'
state: present
secret_type: 'string'
secret: "{{ json_string['stdout'] | string }}"

print-string.py

import json

data = {
"data": "[{'URL': 'beta.test.net', 'token': 'beta-token'}, {'token': 'beta-prod-token', 'URL': 'beta-prod.test.net'}]"
}

print(json.dumps(data).replace('[{\'', '[{\\"').replace('\': \'', '\\": \\"').replace('\'', '\\"'))

will produce:

{
"data": "[{\"URL\": \"beta.test.net\", \"token\": \"beta-token\"}, {\"token\": \"beta-prod-token\", \"URL\": \"beta-prod.test.net\"}]"
}

enter image description here

Jinja2 rendering issues with json values that contains both quotes and double quotes

{'k': '"v1"=\'v2\''} is neither valid JSON nor YAML. This is because JSON exclusively supports double-quoted strings, and YAML does support single-quoted strings but those do not process escape sequences.

Jinja renders it this way because it is valid Python. Jinja does not know you want to parse it as JSON or YAML. You have to serialize the structure as JSON if you want to load it as JSON again later:

import json
from jinja2 import Environment

j = json.loads('{"k": "\\"v1\\"=\'v2\'"}')
t = Environment().from_string('{{ d }}')
r = t.render({'d': json.dumps(j)})
# print(r)=> {"k": "\"v1\"='v2'"}
y = json.loads(r)
print(y)

As you can see, I replaced j with json.dumps(j) so that Jinja will output proper JSON – you can see that r now contains two double-quoted strings.

In this example, you wouldn't even need to load the original string into j as you can just pass it directly to Jinja, but I assume you want to modify the data before passing it into the template.



Related Topics



Leave a reply



Submit