<p>I have 2 templates in my Ansible playbook:</p>
<p><em>template-1.yaml.j2</em>:</p>
<pre><code>{
    &quot;auths&quot;: {
        &quot;https://index.docker.io/v1/&quot;: {
            &quot;auth&quot;: {{ (user.name + ':' + user.pass) | b64encode }}
        }
}
</code></pre>
<p><em>template-2.yaml.j2</em>:</p>
<pre><code>---
apiVersion: v1
data:
  .dockerconfigjson: '{% include &quot;template-1.yaml.j2&quot; %}'
kind: Secret
metadata:
  name: pull-secret
  namespace: default
type: kubernetes.io/dockerconfigjson
</code></pre>
<p>The nested template works great, but I want <code>template-2.yaml.j2</code> to encode <code>template-1.yaml.j2</code> in <strong>base64</strong>, I tried adding a filter like so:</p>
<p><code>{% include &quot;template-1.yaml.j2&quot; | b64encode %}</code></p>
<p>And it fails with the following error:</p>
<p><code>An unhandled exception occured while running the lookup plugin 'template'. Error was a &lt;class 'jinja2.exceptions.TemplateNotFound'&gt;, original message: dGVtcGxhdGUtMi55YW1sLmoy</code></p>
<p>What actually happens is that the literal string <code>template-2.yaml.j2</code> is encoded and fails.</p>
<p>Is there any solution beside separating the templates to different tasks in my Ansible playbook?</p>

Guy Steinberger
<p>Simply replace the following line (wrong):</p>
<pre class="lang-yaml prettyprint-override"><code>.dockerconfigjson: '{% include &quot;template-1.yaml.j2&quot; | b64decode %}'
</code></pre>
<p>with (right):</p>
<pre class="lang-yaml prettyprint-override"><code>.dockerconfigjson: '{{ lookup(&quot;template&quot;, &quot;template-1.yaml.j2&quot;) | b64encode }}'
</code></pre>
<p>The <a href="https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_lookup.html" rel="nofollow noreferrer"><code>template</code> lookup</a> will fetch the result of the given template as a string which can then be passed to the <code>b64encode</code> filter</p>
