Hi, Let us understand how to modify scope using 'with' . I can create a block of restricted scope using with
keyword. I can have a with and then a pipeline where
It's going to evaluate a condition or it's going to read a collection of values.
And once the condition is evaluated, it's going to execute the collection of statements within the block.
And the value that got evaluated using the pipeline will be available or can be used only within that
specific block, within the restricted scope.
Let us understand, using a small demo. Let me get into the chart that we had created.
Let me add some sample values.
I'm going to edit the values file. Here
I'm going to add a key called tags and it's going to have a collection of key value pair like machine as key
and frontdrive as value
Like that I have some key and value pair within the key tags.
So this is something like a map.
Or we can visualize this as a collection as well.
Now I'm going to change the template. So within the template
I'm going to add the with condition and I'm going to read the values.tags, observe the space whitespace.
So I do have two white spaces and the condition is getting started over here.
And the key value pair, I am adding it in the same indent where I am doing the evaluation with
I am adding the keyword then I'm getting the values.tags
So all the values within the tags will be available only within this specific scope and within
this scope, the global within this specific scope
The builtin objects will also be not accessible.
We will do that as a separate demo.
Now I'm going to use this specific values.tag as a with and directly
I can access.machine because the parent scope of this particular block is tags. So within the tags
I can access the machine, rack, dry and Vcard. Let me go ahead and execute the dry run.
And the entire content of this particular YAML file as well as the commands
It will be added along with the resources so directly you can download and start using them.
Now, let me go ahead and do a dry run.
So the condition got evaluated and it read the Values SSD, frontdrive, 4c and 8g and it got printed here.
Now let's go ahead and try another scenario of how the indent can be used.
Getting into the template here the same values from the tags
I'm going to read and add it as a part of the labels.
This is another good example to understand how the white space should be used, so within labels, I
am going to have a set of labels that is first, second and third.
The values will be picked because I am using with within this particular block.
So it's going to read all the values from the tags and all the references within this particular block
will be to that particular tags scope only.
So within the tags, it's going to read machines, rack and dry.
And with the same indent, it's going to print the values, let me go ahead and do a dry run.
The dry run will be very useful for debugging as well.
So within labels, I do have three labels with the key and value and it got added. So the whitespace
Indent is very, very important.
Now, let's go ahead and add something which is not there within the scope.
So within this specific block, I'm going to access the builtin object release.name
So within the tags I will not have anything with release or name.
So there should not be any value getting printed for this.
Let me go ahead and do a dry run. Its not getting any value.
So the yaml it turned out to be a not valid template, and there are some options to solve this
particular problem and definitely there will be a need to access the builtin objects within the
blocks and we can solve it using variables.
We will be discussing about variables in future lecture.
And I can go ahead and add this particular release name.
outside the restricted block and that should work.
If I add this particular release name outside this restricted block
this should work. Let me go ahead and do a dry run.
And yes, it's able to read the release name.
So basically within the restricted block, we cannot use the values from other scope or even the builtin objects.
We cannot access and that should be accessed using the variables.
And this restricted block will be very useful whenever I wanted to
iterate over a given set of values
or given set of arrays it will be very useful so that I can work only on that restricted set of values.