<p>I am using ansible version 2.7 for kubernetes deployment. 
For sending logs to datadog on kubernetes one of the way is to configure annotations like below,</p>

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

<p>this works fine and I could see logs in DataDog.</p>

<p>However I would like to achieve above configuration via ansible deployment on kubernetes for which I have used below code</p>

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

<p>and datadog.json.j2 looks like below</p>

<pre><code>'[{{ '{' }}"source":"{{ sourcea }}"{{ ',' }} "service":"{{ serviceb }}"{{ '}' }}]'  **--&gt; sourcea and serviceb are defined as vars**
</code></pre>

<p>However the resulting config on deployment is below</p>

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

<p>and this config does not allow datadog agent to parse logs failing with below error</p>

<pre><code>[ 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
</code></pre>

<p>if I use ansible code as below (using replace) </p>

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

<p>it generates deployment config as below</p>

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

<p>Which also fails,</p>

<p>to configure the working config with ansible, I have to either remove leading pipe (|) or three quotes coming when using replace). </p>

<p>I would like to have jinja variables substitution in place so that I could configure deployment with desired source and service at deployment time.</p>

<p>kindly suggest</p>

Ruchir Bharadwaj
<p>By introducing space in datadog.json.j2 template definition .i.e.</p>

<pre><code> [{"source":"{{ sourcea }}"{{ ',' }} "service":"{{ serviceb }}"}] (space at start)
</code></pre>

<p>and running deployment again I got the working config as below </p>

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

<p>However I am not able to understand the behaviour if anyone could help me understand it</p>
