首页 > 解决方案 > 我们可以忽略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 }}. Reported by instance {{ $labels.instance
        }} of job {{ $labels.job }}.'
      summary: OOM happens

我在以下两个方面尝试了不同的形式:

但都没有奏效。

遇到同样的错误:

Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: unexpected char u'$' at 213

任何帮助将不胜感激,谢谢:)

标签: ansible

解决方案


你想要的是{% raw %}关闭{% endraw %}Jinja2 模板评估:

- debug:
    msg: >-
     {% raw %}this is some golang {{ $and can have whatever }}{% endraw %}

我相信也可以将 jinja2 转义字符从胡须上移开,但我从未亲自尝试过它有多容易(或者,当然,它是否真的有效)。


推荐阅读