如何转义.yml字典中的单引号

问题描述 投票:0回答:1

我有一个包含 dict 项目的 .yml 文件:

messages_to_skip:
  - service: "test1"
    message: "Exception during timer operation execution"
  - service: "test2"
    message: "Error in pipe 'cpl.service.pipe"
  - service: "test3"
    message: "Unknown attribute with JSONPath"

当我尝试在 jinja 模板中使用它时

messages_to_skip = [
for message_to_skip in messages_to_skip %}
message_to_skip['service'] }}", "{{ message_to_skip['message'] }}"] {% if not loop.last %}, {% endif %}
endfor %}
> ]

我收到一个错误,与此处的单引号相关(在 test2 中):“pipe 'controlpanel”。 我尝试使用反斜杠,但什么也没有。 请问可以帮忙吗?

我尝试使用反斜杠

ansible jinja2
1个回答
0
投票

这应该有效。
您使用过滤器转义引用:

replace("'", "\\'")

messages_to_skip = [
{% for message_to_skip in messages_to_skip %}
  ["{{ message_to_skip.service }}", "{{ message_to_skip.message|replace("'", "\\'") }}"]{% if not loop.last %}, {% endif %}
{% endfor %}
]
© www.soinside.com 2019 - 2024. All rights reserved.