我们可以忽略ansible中内容的格式并将其写入文件吗?
我要存储在文件中的文件内容类似于:我们可以忽略ansible中内容的格式并将其写入文件吗?,ansible,Ansible,我要存储在文件中的文件内容类似于: groups: - name: textfile_collector_alert.rules rules: - alert: service_oom expr: service_oom_file == 1 for: 1m labels: severity: critical annotations: description: 'Hprof files: {{ $labels.file }}. Re
groups:
- name: textfile_collector_alert.rules
rules:
- alert: service_oom
expr: service_oom_file == 1
for: 1m
labels:
severity: critical
annotations:
description: 'Hprof files: {{ $labels.file }}. Reported by instance {{ $labels.instance
}} of job {{ $labels.job }}.'
summary: OOM happens
我在以下两个方面尝试了不同的形式:
in和printf
in内容副本
Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected char u'$' at 213
错误是,原始消息:模板字符串时模板错误:在213处出现意外字符u'$
感谢您的帮助:)您需要的是
{%raw%}
和{%endraw%}
关闭Jinja2模板评估:
- debug:
msg: >-
{% raw %}this is some golang {{ $and can have whatever }}{% endraw %}
我相信也可以将jinja2转义字符从胡须中切换出来,但我个人从未尝试过这有多容易(或者,当然,它是否真的有效)。目前,我还没有合适的解决方案来处理Ansible问题中提到的格式问题(没有Ansible方面的经验) 具有如下解决方案: 使用Java写入本地文件 使用Ansible将本地文件复制到远程
感谢您的帮助,我刚刚使用
shell:printf{%raw%}{{{ruleContent}}{%endraw%}>“{{tmpFolder}}/{{tmpRuleFile}”;
,但现在的内容只是ruleContent
,而此变量的值没有按预期检索。可能我没有理解您试图解决的问题;在您的示例中,您有golang模板文本,这肯定不应该由jinja2解释(从而导致您的“意外字符u'$”在213处”消息).但是现在你正试图打印一些东西--一个绝对糟糕的想法--并期望对规则内容进行评估,即使你明确地将它放在{%raw%}
中?很抱歉尝试不成功,我只是试图将文件内容写入临时文件,仅此而已。我不知道jinja2是什么(尽管读了一些关于它的帖子)。
public static boolean writeToLocal(String absFilePath, String content) {
File file = new File(absFilePath);
file.getParentFile().mkdirs();
try {
Files.write(file.toPath(), Arrays.asList(content));
} catch (IOException ignored) {
ignored.printStackTrace();
return false;
}
return true;
}
- name: Copy local file to remote
copy:
src: "{{ tmpLocalRuleFile }}"
dest: "{{ tmpFolder }}/{{ tmpRuleFile }}"
become: true