<p>In a shell scrip I want to assigning a variable what to use in a value in a deployment. For the life of me I can not figure out how to get it to work.</p>
<p>My helm deploy script file has the following in order to set the value to use my variable :</p>
<pre><code>   --set AuthConfValue=$AUTH_CONF_VALUE
</code></pre>
<p>And I have this in the deployment.yaml file in order to use the variable :</p>
<pre><code>   - name: KONG_SETTING

   value: &quot;{ {{ .Values.AuthConfValue }} }&quot;
</code></pre>
<p>If I assign the variable in my shell script like the following :</p>
<pre><code>   AUTH_CONF_VALUE=&quot;ernie&quot;
</code></pre>
<p>It will work and the value in the deployment will show up like so:</p>
<pre><code>   value: '{ ernie }'
</code></pre>
<p>Now if I try to assign the variable like this:</p>
<pre><code>   AUTH_CONF_VALUE=&quot;\\\&quot;ernie\\\&quot;:\\\&quot;123\\\&quot;&quot;
</code></pre>
<p>I will then get the error <code>error converting YAML to JSON: yaml: line 118: did not find expected key</code> when the helm deploy runs.</p>
<p>I was hoping that this would give me the following value in the deployment :</p>
<pre><code>   value: &quot;{ &quot;ernie&quot;:&quot;123&quot; }&quot;
</code></pre>
<p>If I hardcode the value into the deployment.yaml with this:</p>
<pre><code>   - name: KONG_SETTING

   value: &quot;{ \&quot;ernie\&quot;: \&quot;123\&quot; }&quot;
</code></pre>
<p>and then run the helm deploy it will work and populate the value in the deployment with this -</p>
<pre><code>   value: &quot;{ &quot;ernie&quot;:&quot;123&quot; }&quot;
</code></pre>
<p>Can someone show me if/how I might be able to do this?</p>

ErnieAndBert
<p>The Helm <a href="https://docs.helm.sh/docs/intro/using_helm/#the-format-and-limitations-of---set" rel="nofollow noreferrer"><code>--set</code> option</a> also uses backslash escaping.  So in your example, the <code>$AUTH_CONF_VALUE</code> variable in the host shell contains a single backslash before each quote, which is consumed by <code>--set</code>, so <code>.Values.AuthConfValue</code> contains no backslashes at all, and you get invalid YAML.</p>
<p>If you want to keep this as close to the existing form as you can, let's construct a string with no backslashes at all (and hopefully no commas or brackets either, since those also have special meaning to <code>--set</code>)</p>
<pre class="lang-bash prettyprint-override"><code>AUTH_CONF_VALUE='&quot;ernie&quot;:&quot;123&quot;'
helm install --set AuthConfValue=&quot;$AUTH_CONF_VALUE&quot; .
</code></pre>
<p>When Helm expands a template it doesn't know anything about the context where it might be used.  In your case, you know</p>
<ol>
<li><code>.Values.AuthConfValue</code> is the body of a JSON object</li>
<li>If you surround it in curly braces <code>{ ... }</code> then it should be a valid JSON object</li>
<li>You need to turn that into a correctly-escaped YAML string</li>
</ol>
<p>Helm contains a lightly-documented <code>toJson</code> function that takes an arbitrary object and converts it to JSON; any valid JSON is also valid YAML.  So the closest-to-what-you-have approach might look like</p>
<pre class="lang-yaml prettyprint-override"><code>- name: KONG_SETTING
  value: {{ printf &quot;{%s}&quot; .Values.AuthConfValue | toJson }}
</code></pre>
<hr />
<p>If you're willing to modify your deploy process a little more, you can have less escaping and more certainty.  In the sequence above, we have a string that happens to be a JSON object; what if we had an actual object?  Imagine settings like</p>
<pre class="lang-yaml prettyprint-override"><code># kong-auth.yaml
authConf:
  ernie: &quot;123&quot;
</code></pre>
<p>You could provide this file at install time with a <code>helm install -f</code> option.  Since valid JSON is valid YAML, again, you could also provide a JSON file here without changing anything.</p>
<pre class="lang-bash prettyprint-override"><code>helm install -f kong-auth.yaml .
</code></pre>
<p>Now with this setup <code>.Values.authConf</code> is an object; the only escaping you need to do is standard YAML/JSON escaping (for example quoting <code>&quot;123&quot;</code> so it's a string and not a number).  Now we can use <code>toJson</code> <em>twice</em>, once to get the <code>{&quot;ernie&quot;:&quot;123&quot;}</code> JSON object string, and a second time to escape that string as a value <code>&quot;{\&quot;ernie\&quot;:\&quot;123\&quot;}&quot;</code>.</p>
<pre class="lang-yaml prettyprint-override"><code>- name: KONG_SETTING
  value: {{ .Values.authConf | toJson | toJson }}
</code></pre>
<p>Setting this up would require modifying your deployment script, but it would be much safer against quoting and escaping concerns.</p>
