<p>I need to loop through a list of instances and create 1 stateful set for every instance. However, inside range I then limit myself to the scope of that loop. I need to access some global values in my statefulset.</p>

<p>I've <em>solved</em> it by just putting all global objects I need in an env variable but... this very seems hacky.</p>

<p>What is the correct way to loop through ranges while still being able to reference global objects?</p>

<p>Example of my loop</p>

<pre><code>{{- $values := .Values -}}
{{- $release := .Release -}}

{{- range .Values.nodes }}

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $release.Name }} &lt;-- Global Scope
  labels:
    .
    .
    .    
        env:
          - name: IP_ADDRESS
            value: {{ .ip_address }} &lt;-- From range scope
    .
    .
    .
{{- end }}
</code></pre>

<p>Example of values</p>

<pre><code># Global
image:
  repository: ..ecr.....

# Instances
nodes:

  - node1:
    name: node-1
    iP: 1.1.1.1
  - node2:
    name: node-2
    iP: 1.1.1.1
</code></pre>

<p>When entering a loop block you lose your global context when using <code>.</code>. You can access the global context by using <code>$.</code> instead.</p>
<p>As written in the <a href="https://helm.sh/docs/chart_template_guide/variables/#helm" rel="noreferrer">Helm docs</a> -</p>
<blockquote>
<p>there is one variable that is always global - $ - this variable will always point to the root context. This can be very useful when you are looping in a range and need to know the chart's release name.</p>
</blockquote>
<p>In your example, using this would look something like:</p>
<pre><code>{{- range .Values.nodes }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ $.Release.Name }}
  labels:
    .
    .
    .    
        env:
          - name: IP_ADDRESS
            value: {{ .ip_address }}
    .
    .
    .
{{- end }}
</code></pre>
