【问题标题】:Issue in placing DataDog log annotation on deployment via ansible通过 ansible 在部署中放置 DataDog 日志注释的问题
【发布时间】:2019-02-21 16:14:22
【问题描述】:

我正在使用 ansible 2.7 版进行 kubernetes 部署。 要将日志发送到 kubernetes 上的 datadog,其中一种方法是配置如下注释,

template:
    metadata:
      annotations:
        ad.datadoghq.com/nginx.logs: '[{"source":"nginx","service":"webapp"}]'

这很好,我可以在 DataDog 中看到日志。

但是我想通过在 kubernetes 上的 ansible 部署来实现上述配置,我使用了下面的代码

 template:
        metadata:
           annotations:
             ad.datadoghq.com/xxx.logs: "{{ lookup('template', './datadog.json.j2')}}"

datadog.json.j2 如下所示

'[{{ '{' }}"source":"{{ sourcea }}"{{ ',' }} "service":"{{ serviceb }}"{{ '}' }}]'  **--> sourcea and serviceb are defined as vars**

然而,部署的结果配置如下

template:
    metadata:
      annotations:
        ad.datadoghq.com/yps.logs: |
          '[{"source":"test", "service":"test"}]'

并且此配置不允许 datadog 代理解析失败并出现以下错误的日志

[ AGENT ] 2019-xx-xx xx10:50 UTC | ERROR | (kubelet.go:97 in parseKubeletPodlist) | Can't parse template for pod xxx-5645f7c66c-s9zj4: could not extract logs config: in logs: invalid character '\'' looking for beginning of value

如果我使用下面的 ansible 代码(使用替换)

template:
        metadata:
           annotations:
             ad.datadoghq.com/xxx.logs: "{{ lookup('template', './datadog.json.j2', convert_data=False) | string | replace('\n','')}}"

它生成如下部署配置

template:
    metadata:
      annotations:
        ad.datadoghq.com/yps.logs: '''[{"source":"test", "service":"test"}]'''
      creationTimestamp: null
      labels:

这也失败了,

要使用 ansible 配置工作配置,我必须删除前导管道 (|) 或使用替换时出现的三个引号)。

我想替换 jinja 变量,以便在部署时使用所需的源和服务配置部署。

建议

【问题讨论】:

    标签: kubernetes ansible jinja2 datadog


    【解决方案1】:

    通过在datadog.json.j2模板定义.i.e.中引入空格

     [{"source":"{{ sourcea }}"{{ ',' }} "service":"{{ serviceb }}"}] (space at start)
    

    再次运行部署,我得到了如下的工作配置

    template:
        metadata:
          annotations:
            ad.datadoghq.com/yps.logs: ' [{"source":"test", "service":"test"}]'
    

    但是,如果有人可以帮助我理解这种行为,我无法理解它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 2023-01-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多