I have 2 templates in my Ansible playbook:
template-1.yaml.j2:
{
"auths": {
"https://index.docker.io/v1/": {
"auth": {{ (user.name + ':' + user.pass) | b64encode }}
}
}
template-2.yaml.j2:
---
apiVersion: v1
data:
.dockerconfigjson: '{% include "template-1.yaml.j2" %}'
kind: Secret
metadata:
name: pull-secret
namespace: default
type: kubernetes.io/dockerconfigjson
The nested template works great, but I want template-2.yaml.j2
to encode template-1.yaml.j2
in base64, I tried adding a filter like so:
{% include "template-1.yaml.j2" | b64encode %}
And it fails with the following error:
An unhandled exception occured while running the lookup plugin 'template'. Error was a <class 'jinja2.exceptions.TemplateNotFound'>, original message: dGVtcGxhdGUtMi55YW1sLmoy
What actually happens is that the literal string template-2.yaml.j2
is encoded and fails.
Is there any solution beside separating the templates to different tasks in my Ansible playbook?
Simply replace the following line (wrong):
.dockerconfigjson: '{% include "template-1.yaml.j2" | b64decode %}'
with (right):
.dockerconfigjson: '{{ lookup("template", "template-1.yaml.j2") | b64encode }}'
The template
lookup will fetch the result of the given template as a string which can then be passed to the b64encode
filter