<p>I am trying to figure out how to escape these pieces of a yml file in order to use with helm.  </p>

<pre><code>            - name: SYSLOG_TAG
              value: '{{ index .Container.Config.Labels "io.kubernetes.pod.namespace" }}[{{ index .Container.Config.Labels "io.kubernetes.pod.name" }}]'
            - name: SYSLOG_HOSTNAME
              value: '{{ index .Container.Config.Labels "io.kubernetes.container.name" }}'
</code></pre>

<p>The yml file is a DaemonSet for sending logs to papertrail with instructions here for a standard kubernetes manual deployment <a href="https://help.papertrailapp.com/kb/configuration/configuring-centralized-logging-from-kubernetes/" rel="nofollow noreferrer">https://help.papertrailapp.com/kb/configuration/configuring-centralized-logging-from-kubernetes/</a> .  Here is a link to the full yml file <a href="https://help.papertrailapp.com/assets/files/papertrail-logspout-daemonset.yml" rel="nofollow noreferrer">https://help.papertrailapp.com/assets/files/papertrail-logspout-daemonset.yml</a> .    </p>

<p>I found some answers on how to escape the curly braces and quotes, but still can't seem to get it to work.  It would be easiest if there was some way to just get helm to not evaluate each entire value.  </p>

<p>The last I tried was this, but still results in an error.   </p>

<pre><code>              value: ''"{{" index .Container.Config.Labels \"io.kubernetes.pod.namespace\" "}}"["{{" index .Container.Config.Labels \"io.kubernetes.pod.name\" "}}"]''
            - name: SYSLOG_HOSTNAME
              value: ''"{{" index .Container.Config.Labels \"io.kubernetes.container.name\" "}}"''
</code></pre>

<p>This is the error:</p>

<pre><code>Error: UPGRADE FAILED: YAML parse error on templates/papertrail-logspout-daemonset.yml: error converting YAML to JSON: yaml: line 21: did not find expected key
</code></pre>

<p>I can hardcode values for both of these and it works fine.  I don't quite understand how these env variables work, but what happens is that logs are sent to papertrail for each pod in a node with the labels from each of those pods.  Namespace, pod name, and container name.   </p>

<pre><code>          env:
            - name: ROUTE_URIS
              value: "{{ .Values.backend.log.destination }}"
{{ .Files.Get "files/syslog_vars.yaml" | indent 13 }}
</code></pre>

Corey
<p>Two sensible approaches come to mind.</p>

<p>One is to define a template that expands to the string <code>{{</code>, at which point you can use that in your variable expansion.  You don't need to specially escape <code>}}</code>.</p>

<pre><code>{{- define "cc" }}{{ printf "{{" }}{{ end -}}
- name: SYSLOG_HOSTNAME
  value: '{{cc}} index .Container.Config.Labels "io.kubernetes.container.name" }}'
</code></pre>

<p>A second approach, longer-winded but with less escaping, is to create an external file that has these environment variable fragments.</p>

<pre><code># I am files/syslog_vars.yaml
- name: SYSLOG_HOSTNAME
  value: '{{ index .Container.Config.Labels "io.kubernetes.container.name" }}'
</code></pre>

<p>Then you can <a href="https://helm.sh/docs/chart_template_guide/#accessing-files-inside-templates" rel="nofollow noreferrer">include the file</a>.  This doesn't apply any templating in the file, it just reads it as literal text.</p>

<pre><code>env:
{{ .Files.Get "files/syslog_vars.yaml" | indent 2 }}
</code></pre>

<p>The important point with this last technique, and the problem you're encountering in the question, is that Helm reads an arbitrary file, expands all of the templating, and then tries to interpret the resulting text as YAML.  The <code>indent 2</code> part of this needs to match whatever the rest of your <code>env:</code> block has; if this is deep inside a deployment spec it might need to be 8 or 10 spaces.  <code>helm template</code> will render a chart to text without trying to do additional processing, which is really helpful for debugging.</p>
