I have the following setup.
my_var
has the following value.
ansible_facts:
discovered_interpreter_python: /usr/bin/python
invocation:
module_args:
api_key: null
api_version: v1
ca_cert: null
client_cert: null
client_key: null
context: null
field_selectors: []
host: null
kind: Secret
kubeconfig: null
label_selectors: []
password: null
proxy: null
username: null
validate_certs: null
resources:
- apiVersion: v1
data:
a: blah
b: blah
c: blah
kind: Secret
metadata:
name: my_name
type: Opaque
I am using this in a task with a template like this.
- name: "doh"
k8s:
state: present
namespace: "doh"
definition: "{{ lookup('template', 'template.j2') }}"
My template looks like this.
apiVersion: v1
data: "{{ my_var | json_query("resources[?metadata.name=='" + my_name + ".my_string." + some_var + "'].data") | first | to_nice_yaml }}"
kind: Secret
metadata:
name: "blah"
type: Opaque
Unluckily I get this as a result. This is a string and should be plain yaml.
apiVersion: v1
data: "a: blah <-- quote, why?
b: blah
c: blah
" <-- quote, why?
kind: Secret
metadata:
name: "blah"
type: Opaque
Why am I getting quotes around my yaml in Jinja2 and how do I avoid it?
In your template, there are quotes around the yaml:
data: "{{ ... | to_nice_yaml }}"
These quotes are part of your template and will be part of the rendered output.
I think you're confusing Ansible syntax with jinja2 template syntax (likely based on this gotcha from the docs).
This gotcha is not true for jinja2 templates. Everything that is not inside a jinja2 delimited block ({%
, {{
, etc.) will translate to the rendered value.
If you don't want the quotes in the rendered value, just take them out of the template.