<p>I have a Yaml file in a folder as <code>abc.yaml</code> and the content is</p>
<pre><code>metadata:
  test1: apple
  test2: banana
  test3: cat
container:
  image: foo
  text: xyz
variables:
  ojb: one
  meta: two
</code></pre>
<p>and I have another file <code>values.yaml.j2</code> which needs part of the above content.</p>
<pre><code>metadata:
  test4: dog
  test5: elephant
  {{ .... Here I need test1, test2, test3 from the above (abc.yaml).... }}
container:
  name: test
  {{ .... Here I need image and text from the above (abc.yaml) ....}}
variables:
  ping: pong
  {{ ..... Here I need ojb and meta from the above (abc.yaml) .... }}
</code></pre>
<p>When I was exploring Helm go templates, I found, <code>Files.Lines</code> will return line by line. But I need specific lines as I mentioned above.</p>
<p>Any solution with go template wo get the part of other yaml file?</p>

<p>If you know the other file is YAML, Helm contains a lightly-documented <code>fromYaml</code> extension that can parse it.</p>
<pre class="lang-none prettyprint-override"><code>{{- $abc := .Files.Get &quot;abc.yaml&quot; | fromYaml }}
</code></pre>
<p>From there you have a couple of options on how to proceed.  One tool you have is the corresponding, also lightly-documented, <code>toYaml</code> extension that converts an arbitrary structure back to YAML.</p>
<p>So one choice is to directly emit the values you think you need:</p>
<pre class="lang-yaml prettyprint-override"><code>metadata:
  test4: dog
  test5: elephant
  test1: {{ $abc.metadata.test1 }}
  test2: {{ $abc.metadata.test2 }}
  test3: {{ $abc.metadata.test3 }}
</code></pre>
<p>A second is to emit the new values for each block, plus the existing content:</p>
<pre class="lang-yaml prettyprint-override"><code>metadata:
  test4: dog
  test5: elephant
{{ $abc.metadata | toYaml | indent 2 -}}
</code></pre>
<p>A third is to modify the structure in-place, and then ask Helm to write out the whole thing as YAML.  Unusual for Helm template functions, <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#set" rel="nofollow noreferrer"><code>set</code></a> modifies the dictionary it's given in place.</p>
<pre class="lang-yaml prettyprint-override"><code>{{- $_ := $abc.metadata | set &quot;test4&quot; &quot;dog&quot; | set &quot;test5&quot; &quot;elephant&quot; -}}
{{- toYaml $abc -}}
</code></pre>
