<p>I having hard time looping over array values in values.yaml. The Error some kind of syntax/yaml structure related error but the message is so cryptic I have no idea what the issue is:
My values.yaml file:</p>
<pre class="lang-yaml prettyprint-override"><code>agent:
 namespace: &quot;&quot; &lt;values are subject to change&gt;
 enabled: true
 admin_group:
   - &quot;TEST_GROUP_1&quot;
   - &quot;TEST_GROUP_2&quot;
</code></pre>
<p>my template are:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- if .Values.agent.enabled }}
kind: Rolebinding
apiVersion: rbac.authorization.k8s.io/v1
subjects:
  {{- range .Values.agent.admin_group }}
  - kind: group
    apiGroup: rbac. authorization.k8s.io
    name: {{ .Values.agent.admin_group }}
  {{- end}}
roleRef:
  apigroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: view
{{- end -}}
</code></pre>
<p>I would also like to create multiple groups based on the array values of admin_group:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- if .Values.agent.enabled }}
{{- range .Values.agent.admin_group }}
kind: Sync
apiVersion: group.xxx.com/v1alpha1
metadata:
  name: group-sync-{{ .Values.agent.namespace }}
  namespace: {{ .Values.agent.namespace }}
spec:
  domain: external
  group: {{ .Values.agent.admin_group }}
{{- end }}
{{- end -}}
</code></pre>
<p>Really appreciate the help. Happy Holidays!!!</p>

Ricky18
<p>Your problem is with the <em>body</em> of your <em>range</em> loops. When you write this:</p>
<pre><code>{{- range .Values.agent.admin_group }}
...
{{- end}}
</code></pre>
<p>Then you're redefining the meaning of <code>.</code>. Within the loop, <code>.</code> refers to each successive value produced by the <code>range</code> operator. When you write <code>.Values.agent.admin_group</code> <em>inside</em> the loop, you're getting an error because the list items don't have a <code>.Values</code> field (because they're strings). You want to write:</p>
<pre><code>{{- range .Values.agent.admin_group }}
- kind: group
  apiGroup: rbac. authorization.k8s.io
  name: {{ . | quote }}
{{- end}}
</code></pre>
<p>And similarly in your <code>Sync</code> template.</p>
<p>You can find the relevant documentation <a href="https://helm.sh/docs/chart_template_guide/control_structures/#looping-with-the-range-action" rel="nofollow noreferrer">here</a> which includes a number of examples (both on that page and on the subsequence page about variables).</p>
