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

时间:2018-10-22 06:04:53

标签: 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

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

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

目前,我没有适当的解决方案来解决Ansible问题中提到的 格式问题 (没有使用Ansible的经验)。

具有以下解决方案:

使用Java写入本地文件

public class MapWrapper {

    // this needs to be volatile
    private volatile Map<String, MyClass> map;

    // this needs to be synchronized 
    public synchronized fillMap() {
        // you have single access to the map, fill it as needed
    }

    // this needs to be synchronized 
    public synchronized editMap() {
        // you have single access to the map
        Map<String, MyClass> clone = deepCloneMap(map); 
        try {
           // modify the clone map
           // as the last instruction in the try block, reassign references 
           map = clone; 
        } catch (Exception e) {
           // deal with exception, don't reassign the reference to roll back  
        }
    }

}

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

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