gpt4 book ai didi

ansible - 在 Ansible Jinja 模板中,如何转义双引号内的单引号?

转载 作者:行者123 更新时间:2023-12-04 02:45:17 25 4
gpt4 key购买 nike

在 Ansible 角色中,我使用 Jinja 模板创建一个从变量中获取值的文件。

在 jinja 模板中获取变量的 vars 文件 vars/main.yml 的内容:

Header:
- key: a-b-c
action: xxx
option: '"xyz 'ZZZ' abc.de *.abc.de"'
enabled: true

Jinja模板文件templates/file.conf.j2的内容:
{% for item in Header %}
{% if item.enabled is sameas true %}
Header {{ item.action }} {{ item.key }} {{ item.option }}
{% endif %}
{% endfor %}

调用模板模块的任务/main.yml 文件的内容:
- name: create server.conf
template:
src: file.conf.j2
dest: 'mydir/server.conf'
owner: root
group: root
mode: '0644'

但我收到以下错误:
The offending line appears to be:

action: xxx
option: '"xyz 'ZZZ' abc.de *.abc.de"'
^ here
We could be wrong, but this one looks like it might be an issue with
unbalanced quotes. If starting a value with a quote, make sure the
line ends with the same set of quotes. For instance this arbitrary
example:

foo: "bad" "wolf"

Could be written as:

foo: '"bad" "wolf"'

我期望输出文件 mydir/server.conf 中的内容应该是:
Header xxx a-b-c "xyz 'ZZZ' abc.de *.abc.de"

我怎样才能做到这一点?

最佳答案

引用我最喜欢的资源 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."

由于你的外部引用是单一的,你应该这样写你的值:
option: '"xyz ''ZZZ'' abc.de *.abc.de"'
题外话额外回答 :您在模板中的情况似乎很奇怪。您可以简单地检查您的值是否为真,并使用 bool 添加额外的安全性。筛选:
{% if item.enabled | bool %}

关于ansible - 在 Ansible Jinja 模板中,如何转义双引号内的单引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57418999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com