我正在使用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
如果我使用如下的错误代码(使用替换)
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配置工作配置,我必须删除前导管道(|)或使用replace时出现的三个引号。
我想使用jinja变量替换,以便可以在部署时使用所需的源和服务配置部署。
友善建议
最佳答案
通过在datadog.json.j2模板定义中引入空格。
[{"source":"{{ sourcea }}"{{ ',' }} "service":"{{ serviceb }}"}] (space at start)
然后再次运行部署,我得到如下工作配置
template:
metadata:
annotations:
ad.datadoghq.com/yps.logs: ' [{"source":"test", "service":"test"}]'
但是,如果有人可以帮助我理解它,我将无法理解该行为
关于kubernetes - 通过Ansible将DataDog日志注释放置在部署中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54811591/