Hi, Let us understand how to use variables, as we discussed in the earlier lecture, whenever I wanted
to inject a value within a with block, which is out of its scope
I need to use the variable. The same way within the range looping if I wanted to assign or process any
specific element then I need to assign it to variable and process it within the block.
For that, I need to assign a value into a variable.
The syntax for that is the name of the variable with a dollar sign and the special assignment operator
that we will be using is :=
And then the value that we are reading from the template or a literal value.
Let's go ahead and do a small demo and get better understanding.
Let me modify the template.
Within the template, I'm going to add few lines.
So what I'm doing over here, I do have a with block and outside the block
I'm reading the release name and assigning in to a variable called relname.
And that is starting with the dollar sign that is the syntax and the operated to assign the value is :=
And this particular variable can be accessed across the template and within the with block
I am going to read that specific variable that is dollar or relname and print it with the
key release. Earlier when we are accessing this particular value release.name
That is the builtin object I was not able to read.
Now what I am doing, I am assigning it to a variable and that variable will have the scope across the template.
And over here I have added a dash in the beginning as well as in the end, because it's not going to
print anything and if I doesn't provide this particular dash, it's going to add a new line character.
And there will be a new line or empty line within that YAML file.
Even though that is valid, that's not recommended.
So let me go ahead and save this particular file and execute it.
So the execution I'm going to do using a dry run.
It's able to print the release name.
That's the name that I provided as a part of the dry run statement.
And there is no new line character where we are reading or assigning the variable.
All other lines within the with block is same as we had earlier.
And this is a proof where
We can assign a builtin object into a variable and read it within the with block.
Now let us see another demo where
I'm going to assign an element within the range into a variable and read it within the range block.
So within the template let me go ahead and edit the template.
I'm going to remove these lines and add a range block.
So within the range, what I'm doing
I'm assigning that specific element that is values.langused as a part of topping
And I'm going to have the index
index of the element will be getting assigned to this, and I will printing both the index and the
individual element that is being read within the collection.
So I I'll be having key as the index and value as the actual element within the collection.
And this particular collection already we do have within the values file
Anyway we can go ahead and verify once.
So we do have langused.
And I do have a collection of values.
Now I'm going to do a dry run.
So I'm getting the index and the actual value.
This is one another way of reading the variable and as a part of the variable
another important thing to understand is the dollar.
That's a global variable
And this particular variable will always point to the root context.
So I can access the global objects using the variable dollar.
Let us see some demo about dollar variable as well.
I'm going to edit this particular file.
Let me go ahead and add few labels to this.
So as a part of the label let me delete these
So here I am adding the value by accessing the global variable dollar.
So that will have the root context.
So I can access the global object.
Here I am accessing the global object, chart.name, chart.version, release details as well as the
chart appversion. Let me go ahead and execute this particular chart and for almost all the demo
I'm using the dry run because there is no need to go ahead and deploy. If a dry run is good definitely
it will get deployed into the kubernetes cluster. Once we learn each and every individual concept, we can consolidate
everything together and release them into the kubernetes cluster.
I'm going to do a dry run of the chart mychart.
So I'm able to get the chart releases and release
Name, the version of the helm that we're using and what it is managed by
That is the release service all the information we're able to get.
So in a quick summary we learnt three major concepts.
That is, how to inject a variable into a with block and how to assign a individual element of a
range and access the individual element and its index within the range looping and how to access the
global variable dollar and use the global objects.