代码之家  ›  专栏  ›  技术社区  ›  Hearen

我们可以忽略ansible中内容的格式并将其写入文件吗?

  •  0
  • Hearen  · 技术社区  · 7 年前

    我要存储在文件中的文件内容如下:

    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
    

    如有任何帮助,我们将不胜感激,谢谢:)

    2 回复  |  直到 7 年前
        1
  •  0
  •   mdaniel    7 年前

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

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

    我相信也有可能把金贾2的逃亡人物从胡子上转移开,但我从来没有亲自尝试过去了解这有多容易(当然,也没试过它是否真的有效)。

        2
  •  0
  •   Hearen    7 年前

    目前,我没有合适的解决方案来处理 格式问题 在回答问题中提到(没有回答问题的经验)。

    解决方案如下:

    用Java编写本地文件

    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;
    }
    

    使用ansible将本地文件复制到远程

    - name: Copy local file to remote
      copy:
        src: "{{ tmpLocalRuleFile }}"
        dest: "{{ tmpFolder }}/{{ tmpRuleFile }}"
      become: true