0

I have a variable say :

{{- $var := 7 -}}

I need to perform an arithmetic operation involving this variable and store the result back into the same i.e for example

var = var + 2

I tried doing :

{{- $var := add ( {{ $var }} 2 ) -}}

but doesn't seem to work, throws this error

unexpected "{" in parenthesized pipeline
David Maze
  • 130,717
  • 29
  • 175
  • 215

1 Answers1

0

It should be (see add & documentation):

{{- $var := add $var 2 -}}

For example

apiVersion: v1
kind: ConfigMap
metadata:
    name: myconfigmap
data:
  {{ $var := 5  }}
  prevVal: {{ $var }}
  {{ $var = add $var 2 }}
  newVal: {{ $var }}

Rendered:

apiVersion: v1
kind: ConfigMap
metadata:
    name: myconfigmap
data:
  
  prevVal: 5
  
  newVal: 7

= is to assign a variable and := is define and assign a variable.

Fcmam5
  • 4,888
  • 1
  • 16
  • 33
  • 1
    we use ":=" for redeclaration right? so in order to re-use the same "var" the next time it should be involved in an "=" operation instead of ":=" right? – Aditya Vashist Feb 01 '23 at 12:21
  • Sorry, I fixed my example above, := is to declare and assign a variable. See: https://stackoverflow.com/a/73091638/5078746 – Fcmam5 Feb 01 '23 at 12:59