在Ansible角色中,我使用Jinja模板创建了一个从变量中获取值的文件。
在jinja模板中从中获取变量的vars文件vars / main.yml的内容:
Header:
- key: a-b-c
action: xxx
option: '"xyz 'ZZZ' abc.de *.abc.de"'
enabled: true
Jinja Templet文件模板/file.conf.j2的内容:
{% for item in Header %}
{% if item.enabled is sameas true %}
Header {{ item.action }} {{ item.key }} {{ item.option }}
{% endif %}
{% endfor %}
tasks / 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"
我该如何实现?
答案 0 :(得分:2)
引用我喜欢的资源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 %}