25

I would like to pass array as property in yaml (values file) in Helm. What I tried:

  1. Attempt.

    elasticsearch:
      uri: "[\"127.0.0.1:9200\",\"127.0.0.2:9200\"]"
    

    Error:

    ReadString: expects " or n, but found [, error found in #10 byte of ...|RCH_URL":["127.0.0.1|..., bigger context ...|{"apiVersion":"v1","data":{"ELASTIC_SEARCH_URL": ["127.0.0.1:9200","127.0.0.2:9200"],"LOGS_ENV_PREFI|...

  2. Attempt. According to official helm site how to pass array

    elasticsearch:
      --set uri={127.0.0.1:9200,127.0.0.2:9200}
    

    With error:

    error converting YAML to JSON: yaml: line 15: mapping values are not allowed in this context

  3. Attempt.

     elasticsearch:
       uri: 
       - 127.0.0.1:9200
       - 127.0.0.2:9200
    

    Failed with the same exception as 1.

EDIT: Actually in my case the helm values were not used in YAML file then, so I needed another format and finally solution was to pass uri as string with single quote:

 elasticsearch:
   uri: '["127.0.0.1:9200","127.0.0.2:9200"]'

Nevertheless @Marcin answer was correct.

Bartek
  • 2,109
  • 6
  • 29
  • 40
  • Helm Rendering of values from values.yaml to config.yaml : values.yaml : sites: - dataprovider: abcd - dataprovider: xyzx config.yaml : sites: {{ toYaml .Values.sites | indent 10 }} – Sandeep Jain Jan 15 '21 at 07:10

2 Answers2

23

You pass an array of values by using either the old fashioned json way:

elasticsearch:
  uri: ["127.0.0.1:9200", "127.0.0.2:9200"]

or the way introduced by yaml:

elasticsearch:
  uri: 
  - 127.0.0.1:9200
  - 127.0.0.2:9200

You can then access the values in Helm templates using range:

Uris:{{- range .Values.elasticsearch.uri }}
{{.}}{{- end }}

resolves to:

Uris:
127.0.0.1:9200
127.0.0.2:9200
Marcin Król
  • 1,555
  • 2
  • 16
  • 31
22

Helm Rendering of values from values.yaml to config.yaml :

values.yaml:

sites:
  - dataprovider: abcd
  - dataprovider: xyzx

config.yaml:

sites:
  {{ toYaml .Values.sites | indent 10 }}
EugZol
  • 6,476
  • 22
  • 41
Sandeep Jain
  • 1,019
  • 9
  • 13
  • 2
    This worked for me after a small change -- the first line of output was indented 12 spaces instead of 10; I had to fully left justify the second line above to get the expected output. – Donnie C Oct 04 '21 at 19:59
  • 7
    One could also write it on one line as `sites: {{ toYaml .Values.sites | nindent 10 }}`. `nindent` adds a leading newline. – tlwhitec Oct 05 '21 at 11:02
  • Yep we can do these changes as suggested on the requirement of the output. – Sandeep Jain Jan 21 '22 at 16:39