0

I was running into this particularly painful Ansible task of:

  1. Reading JSON from a file.
  2. Passing the JSON as a string to helm, BUT not quoting it.
- name: deploy release
  community.kubernetes.helm:
    name: my_release
    chart_ref: ./charts/my_chart
    release_namespace: "{{namespace}}"
    state: "{{state}}"
    release_values:
          x: "{{ lookup('file', './stuff.json') }}"

What I want the helm values file to look like is:

x: |
  { "hello": "world" }

The issue I ran into with the following lookup {{ lookup('file', './stuff.json') }} is that ansible will interpret it as a dict and pass the dict to helm. This does not work as I need a string. Here's what the output in the helm values file looks like:

x:
  hello: world

Then I tried {{ lookup('file', './stuff.json') | quote}}. Ansible passes a string to helm, but that string has a quote around it. When I try to read the JSON in my deployment, I get a parse error. Here's what the output would look like:

x: '{ "hello": "world" }'

I even tried {{ lookup('file', './stuff.json') | to_json }}, as recommended here, but that failed as well.

Breedly
  • 12,838
  • 13
  • 59
  • 83

1 Answers1

1

Using {{ lookup('file', './stuff.json') | string }} will force Ansible to evaluate it as a string without adding quotes.

There are several examples in Using filters to manipulate data that use this filter.

Documentation for the filter can be found in the Jinja2 documentation. The documentation states that the filter will:

Make a string unicode if it isn’t already. That way a markup string is not converted back to unicode.

I'm not particularly sure why this corrects the issue, but it did.

Breedly
  • 12,838
  • 13
  • 59
  • 83