<p>Why does using Ansible variable make the following Ansible simple task to fail?</p>
<pre><code>- name: Create secret
  kubernetes.core.k8s:
    state: present
    definition:
      apiVersion: v1
      kind: Secret
      metadata:
        name: somename
        namespace: default
      type: Opaque
      stringData:
        management.json: |
          {
            &quot;Proto&quot;: &quot;udp&quot;,
            &quot;URI&quot;: &quot;stun:coturn.{{ domain_name }}:3478&quot;,   &lt;&lt;&lt;=== **HERE**
          }
</code></pre>
<p>Replacing <code>{{ domain_name }}</code> with any string manually like <code>mydomain</code> works just fine!</p>
<p>I am not sure what is wrong when using Ansible variable and how can I solve it?</p>
<p>Here is the error I get when running the playbook:</p>
<pre><code>    &quot;msg&quot;: &quot;Failed to create object: b'{\&quot;kind\&quot;:\&quot;Status\&quot;,\&quot;apiVersion\&quot;:\&quot;v1\&quot;,\&quot;metadata\&quot;:{},\&quot;status\&quot;:\&quot;Failure\&quot;,\&quot;message\&quot;:\&quot;Secret in version \\\\\&quot;v1\\\\\&quot; cannot be handled as a Secret: json: cannot unmarshal object into Go struct field Secret.stringData of type string\&quot;,\&quot;reason\&quot;:\&quot;BadRequest\&quot;,\&quot;code\&quot;:400}\\n'&quot;,
    &quot;reason&quot;: &quot;Bad Request&quot;
</code></pre>
<p>And as said, removing the variable from the definition works.</p>

Mohammed Noureldin
<p>I'd guess you're getting bitten by the ansible+jinja2 auto-coercion behavior due to the leading <code>{</code> in that field. The usual work-around for that is one of two ways, depending on whether the upstream system tolerates any leading whitespace or not</p>
<h3><code>|string</code> filter</h3>
<pre class="lang-yaml prettyprint-override"><code>      stringData:
        management.json: &gt;-
         {{
          {
            &quot;Proto&quot;: &quot;udp&quot;,
            &quot;URI&quot;: &quot;stun:coturn.&quot;+ domain_name +&quot;:3478&quot;,
          } | tojson | string
         }}
</code></pre>
<h3>leading whitespace</h3>
<pre class="lang-yaml prettyprint-override"><code>      stringData:
        management.json: |
          {{&quot; &quot;}}{
            &quot;Proto&quot;: &quot;udp&quot;,
            &quot;URI&quot;: &quot;stun:coturn.{{ domain_name }}:3478&quot;,   &lt;&lt;&lt;=== **HERE**
          }
</code></pre>
<p>One can see the erroneous behavior in a simple test</p>
<pre class="lang-yaml prettyprint-override"><code>  - debug:
      msg: |
        foo is {{ foo }} of type {{ foo | type_debug }}
    vars:
      foo: |
        {
          &quot;Proto&quot;: &quot;udp&quot;,
          &quot;URI&quot;: &quot;coturn.{{ inventory_hostname }}:3478&quot;
        }
</code></pre>
<p>which as written emits</p>
<pre><code>    &quot;msg&quot;: &quot;foo is {'Proto': 'udp', 'URI': 'coturn.localhost:3478'} of type dict\n&quot;
</code></pre>
<p>but with the same <code>{{&quot; &quot;}}</code> patch applied emits</p>
<pre><code>    &quot;msg&quot;: &quot;foo is  {\n  \&quot;Proto\&quot;: \&quot;udp\&quot;,\n  \&quot;URI\&quot;: \&quot;coturn.localhost:3478\&quot;\n}\n of type str\n&quot;
</code></pre>
<p>and similar, although (of course) without the leading space using the <code>|json|string</code> pipeline</p>
<pre><code>    &quot;msg&quot;: &quot;foo is {\&quot;Proto\&quot;: \&quot;udp\&quot;, \&quot;URI\&quot;: \&quot;coturn.localhost:3478\&quot;}\n of type str\n&quot;
</code></pre>
