WEBVTT

1
00:00.000 --> 00:04.880
So friends, now that we know how to use basic variable substitution within our

2
00:04.880 --> 00:06.240
Ginger 2 templates,

3
00:06.240 --> 00:10.080
let's now take a look at a little bit more of an advanced subject, and that is

4
00:10.080 --> 00:10.800
utilizing

5
00:10.800 --> 00:15.030
loops within our templates. So with that said, let's just get to it. Now, one

6
00:15.030 --> 00:15.920
of the best places

7
00:15.920 --> 00:19.600
that I would reference that you go to if you want to learn how to use Ginger

8
00:19.600 --> 00:20.800
templating any more

9
00:20.800 --> 00:25.080
expressive manner, and that is to go to the actual Ginger 2 documentation

10
00:25.080 --> 00:26.560
itself. So if we collect

11
00:26.560 --> 00:30.550
the link for Ginger 2 documentation here. So as we can see here, we are
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:26.560 --> 00:30.550
the link for Ginger 2 documentation here. So as we can see here, we are

12
00:30.550 --> 00:32.240
presented with a table of

13
00:32.240 --> 00:35.850
contents. There are a whole number of different sections that we can explore,

14
00:35.850 --> 00:36.640
but if we scroll

15
00:36.640 --> 00:40.720
down to list of control structures right here and click this link, what it says

16
00:40.720 --> 00:41.520
here is that a

17
00:41.520 --> 00:45.280
control structure refers to all things that control the flow of a program. So

18
00:45.280 --> 00:46.000
we have things like

19
00:46.000 --> 00:49.960
conditional statements, but the thing that we want to be looking at is this one

20
00:49.960 --> 00:51.600
right here, a for loop.

21
00:51.600 --> 00:55.730
So a key point to understand is that when we want to utilize a loop within Ans

22
00:55.730 --> 00:56.080
ible,

23
00:56.080 --> 01:00.320
we just use the loop keyword. But ultimately, Ansible is acting as an
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:56.080 --> 01:00.320
we just use the loop keyword. But ultimately, Ansible is acting as an

24
01:00.320 --> 01:01.760
abstraction layer presenting a

25
01:01.760 --> 01:06.210
yaml interface to interact with the underlying Python code and the underlying

26
01:06.210 --> 01:07.360
Python code is

27
01:07.360 --> 01:11.420
utilizing a for loop. So we can actually see here the syntax is using the

28
01:11.420 --> 01:12.560
keyword for and then we

29
01:12.560 --> 01:17.110
specify a variable here, which can be any name you so wish, although is a good

30
01:17.110 --> 01:18.320
idea and good practice

31
01:18.320 --> 01:23.050
to have something sensible, some descriptive. And then we have the keyword in,

32
01:23.050 --> 01:24.240
and then we target

33
01:24.240 --> 01:28.810
the iterable object that we want to loop over. So an iterable object, that is

34
01:28.810 --> 01:29.760
anything that can be

35
01:29.760 --> 01:33.790
broken down into smaller components, such as an array can be broken down into
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

35
01:29.760 --> 01:33.790
broken down into smaller components, such as an array can be broken down into

36
01:33.790 --> 01:34.800
individual elements

37
01:34.800 --> 01:38.710
within the array. This is what we're talking about here. Now, when we happen to

38
01:38.710 --> 01:39.840
utilize the

39
01:39.840 --> 01:44.160
variable here, given the name user, and then this particular variable here, as

40
01:44.160 --> 01:45.120
denoted by the name

41
01:45.120 --> 01:48.690
user gives us access to each individual element. So if that element also has

42
01:48.690 --> 01:50.160
multiple keys, we can,

43
01:50.160 --> 01:54.580
in this example here, use dot notation to access the username key. Now the

44
01:54.580 --> 01:55.440
interesting thing to

45
01:55.440 --> 01:58.580
note is that if you happen to have experience with Python, this part here looks

46
01:58.580 --> 02:00.160
very, very familiar.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

46
01:58.580 --> 02:00.160
very, very familiar.

47
02:00.160 --> 02:05.030
But with Python, the for loop is going to automatically close itself. In the

48
02:05.030 --> 02:06.480
case of ginger too, that is

49
02:06.480 --> 02:11.770
not true. We actually have to be very explicit and manually close each for loop

50
02:11.770 --> 02:13.520
that we happen to open.

51
02:13.520 --> 02:17.540
And we do this as we can see here with this end for statement, again, within

52
02:17.540 --> 02:18.400
the colubracing

53
02:18.400 --> 02:22.570
percentage signs. So if we happen to have multiple loops, ie nested loops, we

54
02:22.570 --> 02:23.440
're also going to happen

55
02:23.440 --> 02:28.320
to have multiple end for statements. That's kind of the rules here at play. Now

56
02:28.320 --> 02:29.760
, very, very important.

57
02:29.760 --> 02:33.870
Recall with respect to our variable substitution, we could just use our double
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:29.760 --> 02:33.870
Recall with respect to our variable substitution, we could just use our double

58
02:33.870 --> 02:34.720
curly braces.

59
02:34.720 --> 02:38.560
When we want to be using these control structures here within ginger too, we're

60
02:38.560 --> 02:39.600
going to have our

61
02:39.600 --> 02:43.970
curly brace and then our percentage sign. See that? Colubrace percentage. And

62
02:43.970 --> 02:44.960
then the actual control

63
02:44.960 --> 02:47.760
structure, whereas when we're actually referencing the actual variable, which

64
02:47.760 --> 02:48.400
we're looping through

65
02:48.400 --> 02:52.570
here, we still use the double curly braces. That is the gist of it. Now, if you

66
02:52.570 --> 02:53.120
happen to have

67
02:53.120 --> 02:57.120
familiarity with Python, this will be much more natural to you. Whereas if your

68
02:57.120 --> 02:58.000
only exposure is

69
02:58.000 --> 03:01.360
to answerable itself directly, this might take a few tries just to get
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

69
02:58.000 --> 03:01.360
to answerable itself directly, this might take a few tries just to get

70
03:01.360 --> 03:02.960
comfortable. But like I say,

71
03:02.960 --> 03:07.270
with everything, it just is a little bit of practice to get familiar. So let's

72
03:07.270 --> 03:08.400
actually add to our

73
03:08.400 --> 03:13.060
variable files and begin building out a larger configuration file rendered via

74
03:13.060 --> 03:14.320
the use of loops

75
03:14.320 --> 03:18.160
within our logic. So check this out then. So let's go into the host files here

76
03:18.160 --> 03:18.800
and let's go into

77
03:18.800 --> 03:23.000
blue box dot yml. So all I will do is I'll add in some new information. Okay.

78
03:23.000 --> 03:24.000
So let's add in some

79
03:24.000 --> 03:26.900
users here. Now this doesn't actually have to be accurate. Like you say, we're

80
03:26.900 --> 03:27.840
just creating some

81
03:27.840 --> 03:31.590
dummy configurations just to test out how we can use these templates. So let's
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

81
03:27.840 --> 03:31.590
dummy configurations just to test out how we can use these templates. So let's

82
03:31.590 --> 03:32.880
just say on the

83
03:32.880 --> 03:36.840
blue box here, let's imagine we happen to have a user called Ansible, which we

84
03:36.840 --> 03:37.520
actually do.

85
03:37.520 --> 03:40.720
And then we'll specify the group to which Ansible belongs to. So we'll say it

86
03:40.720 --> 03:41.600
belongs to the Ansible

87
03:41.600 --> 03:44.960
group, as well as at the real group. Again, we're not caring about the actual

88
03:44.960 --> 03:45.600
accuracy here,

89
03:45.600 --> 03:49.540
I think they're a little bit off. Let's add a new name. We'll have the nugget

90
03:49.540 --> 03:50.560
user. The group's

91
03:50.560 --> 03:55.090
belonging here is going to be nugget user, as well as let's just say the real

92
03:55.090 --> 03:56.000
group as well.

93
03:56.000 --> 03:59.170
And let's imagine it's also part of the, oh, I don't know, let's say the

94
03:59.170 --> 04:00.320
marketing department.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

94
03:59.170 --> 04:00.320
marketing department.

95
04:00.320 --> 04:04.530
Okay. And let's maybe add another name, let's just say go with James, add to

96
04:04.530 --> 04:06.480
the grips of James.

97
04:06.480 --> 04:09.840
We have the primary James group, James can be part of the real group, and James

98
04:09.840 --> 04:10.720
can be also in the

99
04:10.720 --> 04:14.780
engineer group. Okay. So write and quit, let's go into the yellow box here, let

100
04:14.780 --> 04:15.360
's do a similar

101
04:15.360 --> 04:20.030
thing. So we'll add in the users, add the name of once again the grips will be

102
04:20.030 --> 04:21.280
Ansible, as well as

103
04:21.280 --> 04:25.690
we'll add in the nugget user as well here, add on nugget user and we'll, and

104
04:25.690 --> 04:27.040
let's just say on this

105
04:27.040 --> 04:30.920
particular device, instead of James, we have a user called Knox. In fact, let's
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

105
04:27.040 --> 04:30.920
particular device, instead of James, we have a user called Knox. In fact, let's

106
04:30.920 --> 04:31.760
just add marketing

107
04:31.760 --> 04:34.790
here just for a little bit of consistency, not that it matters. So Knox is in

108
04:34.790 --> 04:35.440
the Knox primary

109
04:35.440 --> 04:39.040
group, Knox is also part of the wheel, and let's say Knox is in the programming

110
04:39.040 --> 04:40.320
department. Okay.

111
04:40.320 --> 04:43.810
So check this out. Let's go back to our template right here. So I want to

112
04:43.810 --> 04:45.040
invoke my loop. Now remember

113
04:45.040 --> 04:48.720
how to do this, curly brace percentage time. And now I specify my loop

114
04:48.720 --> 04:50.560
contracts, I'll say four,

115
04:50.560 --> 04:56.200
and now I can choose a variable of any name I so wish. So I could say for pizza

116
04:56.200 --> 04:57.920
, and the users,

117
04:57.920 --> 05:02.090
then I have my percentage sign and curly brace. And the reality is this is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

117
04:57.920 --> 05:02.090
then I have my percentage sign and curly brace. And the reality is this is

118
05:02.090 --> 05:03.440
absolutely valid.

119
05:03.440 --> 05:07.070
We have our four keyword, the N keyword, and the actual object we're going to

120
05:07.070 --> 05:07.520
look through,

121
05:07.520 --> 05:11.010
which is going to be the users key, which is corresponding here, this

122
05:11.010 --> 05:11.840
particular key here.

123
05:11.840 --> 05:15.630
So that gives us access to each element, which is this one here, the first

124
05:15.630 --> 05:16.160
element,

125
05:16.160 --> 05:19.840
and then the second elements. And then we can call the individual keys within

126
05:19.840 --> 05:20.560
that particular

127
05:20.560 --> 05:24.560
elements of say, for example, upon first iteration, we could grab the name key.

128
05:24.560 --> 05:25.360
The key point to know,

129
05:25.360 --> 05:29.360
though, is that each element in the array is going to be represented with

130
05:29.360 --> 05:30.800
whatever keyword you use
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

130
05:29.360 --> 05:30.800
whatever keyword you use

131
05:30.800 --> 05:34.440
here. Now right now, I'm using pizza just to show how flexible this can be. It

132
05:34.440 --> 05:35.040
's not actually a

133
05:35.040 --> 05:38.060
very good thing because it's not actually descriptive of what we're looking

134
05:38.060 --> 05:38.560
through,

135
05:38.560 --> 05:42.780
not exactly best practice, but it can work just to show you. So if I wanted to

136
05:42.780 --> 05:43.280
actually get the

137
05:43.280 --> 05:47.060
variable here, I could just have my double curly braces and say pizza, which

138
05:47.060 --> 05:48.000
gives me access to

139
05:48.000 --> 05:51.510
each iteration and then call the key that I want to target, which in this case

140
05:51.510 --> 05:52.960
here would be the

141
05:52.960 --> 05:57.200
name key. Okay, this would actually print out the names. Now what am I going to

142
05:57.200 --> 05:58.320
do here? Because I

143
05:58.320 --> 06:02.160
have opened my for loop, I'm going to have to close it. So I'll have my curly
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
05:58.320 --> 06:02.160
have opened my for loop, I'm going to have to close it. So I'll have my curly

144
06:02.160 --> 06:02.720
brace and then

145
06:02.720 --> 06:07.480
a percent and then I will say end for and then again, curly brace percent. This

146
06:07.480 --> 06:08.400
here should be

147
06:08.400 --> 06:12.720
absolutely valid. So let's try this. And if I just press escape, colon wq,

148
06:12.720 --> 06:13.600
writing quits,

149
06:13.600 --> 06:17.680
let's see the back. If I clear the screen and I rerun the same playbook, which

150
06:17.680 --> 06:18.400
we'll call the

151
06:18.400 --> 06:21.950
same template just a modified version of the templates, let's hit enter and see

152
06:21.950 --> 06:22.880
what happens.

153
06:22.880 --> 06:26.720
Okay, we seem to have a change on both devices. Let's go and confirm. So on the

154
06:26.720 --> 06:27.040
yellow box,

155
06:27.040 --> 06:30.710
we're going to the 10 directory that's done LS and great for config. Once again
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

155
06:27.040 --> 06:30.710
we're going to the 10 directory that's done LS and great for config. Once again

156
06:30.710 --> 06:31.440
, that version

157
06:31.440 --> 06:34.910
that be overwritten with the new config and check this out right here, we

158
06:34.910 --> 06:36.480
actually have rendered

159
06:36.480 --> 06:40.090
the correct template. Here is all the original stuff. But down here we have on

160
06:40.090 --> 06:41.120
my system, I have

161
06:41.120 --> 06:45.510
the following users, Ansible, nugget user and Knox. Notice the way we were able

162
06:45.510 --> 06:46.720
to correctly parse

163
06:46.720 --> 06:52.390
out the names, despite using a rather poorly worded variable name and pizza.

164
06:52.390 --> 06:52.880
Okay, didn't

165
06:52.880 --> 06:57.570
actually matter. Again, we go to the blue box here. Let's cd into temp. Let's

166
06:57.570 --> 06:59.200
cat config.txt.

167
06:59.760 --> 07:04.000
This time we have the original text. And then again, we have added on on my
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

167
06:59.760 --> 07:04.000
This time we have the original text. And then again, we have added on on my

168
07:04.000 --> 07:04.640
system, I have

169
07:04.640 --> 07:08.230
the following users, Ansible, nugget user and James this time. Now here is an

170
07:08.230 --> 07:09.120
interesting thing.

171
07:09.120 --> 07:13.220
Suppose you want to have another loop that's going to look through all of the

172
07:13.220 --> 07:13.920
groups that

173
07:13.920 --> 07:18.310
each member belongs to. This is going to require a nested loop. This actually

174
07:18.310 --> 07:19.280
is quite interesting

175
07:19.280 --> 07:23.610
and may be confusing when you first see it because we have two different loops.

176
07:23.610 --> 07:24.160
We're going to have

177
07:24.160 --> 07:28.160
to close both of them with two end four statements. So let's go and check this

178
07:28.160 --> 07:29.520
out. So let's go back

179
07:29.520 --> 07:33.360
into the template. Now what I'm going to do here is I'm going to just change
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

179
07:29.520 --> 07:33.360
into the template. Now what I'm going to do here is I'm going to just change

180
07:33.360 --> 07:34.320
the name of the variable

181
07:34.320 --> 07:37.960
pizza to something that actually is descriptive now I've actually highlighted

182
07:37.960 --> 07:38.560
the ability of

183
07:38.560 --> 07:42.170
being able to choose anything user wish. And I'll just use a much more

184
07:42.170 --> 07:43.360
conventional name such as

185
07:43.360 --> 07:47.250
the word user. So I'll say for user and users. Okay, now I'm going to have to

186
07:47.250 --> 07:48.000
change what I'm

187
07:48.000 --> 07:51.060
calling here and reference the name of the variable so that it's match up. So

188
07:51.060 --> 07:52.480
we have user dot name.

189
07:52.480 --> 07:57.240
As I loop through each of these users, I also want to add in the group

190
07:57.240 --> 07:59.200
membership for each user. Okay,

191
07:59.200 --> 08:01.600
and that was just a little bit of indentations to make things a little bit
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

191
07:59.200 --> 08:01.600
and that was just a little bit of indentations to make things a little bit

192
08:01.600 --> 08:02.400
clearer. And I'll just

193
08:02.400 --> 08:06.470
say groups colon and then I'm going to have my programmatic logic once again.

194
08:06.470 --> 08:07.920
So color brace percent

195
08:07.920 --> 08:12.540
and now I'll say for and again choose any variable use a wish but because it's

196
08:12.540 --> 08:13.200
a little bit more

197
08:13.200 --> 08:16.850
descriptive, I will say group and and what am I actually looping through? Well,

198
08:16.850 --> 08:17.920
I'm actually within

199
08:17.920 --> 08:22.000
a nested loop. So I'm going to have to call the user variable here which access

200
08:22.000 --> 08:23.200
es each elements

201
08:23.200 --> 08:26.340
again before this would have actually been pizza as I discussed but now it's

202
08:26.340 --> 08:27.760
back to being user.

203
08:27.760 --> 08:32.000
And what is the actual key that gives me all of the array objects? I eat all of
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

203
08:27.760 --> 08:32.000
And what is the actual key that gives me all of the array objects? I eat all of

204
08:32.000 --> 08:32.560
the groups what we

205
08:32.560 --> 08:36.150
can see here from the actual documentation. This is the first iteration and if

206
08:36.150 --> 08:37.680
we grab the groups key

207
08:37.680 --> 08:41.680
that gives us access to this iterable object which is answerable and we'll

208
08:41.680 --> 08:42.560
which we'll look through

209
08:42.560 --> 08:46.960
and then upon second iteration we get the nugget user again grab the groups key

210
08:46.960 --> 08:47.680
and then we get

211
08:47.680 --> 08:51.990
this element which we can look through. So we're going to say user dot groups

212
08:51.990 --> 08:52.400
and again we're

213
08:52.400 --> 08:55.680
going to have a percentage sign and color brace and just to make things a

214
08:55.680 --> 08:56.960
little bit more readable

215
08:56.960 --> 09:00.870
I'll have let's say a hyphen and then just give the name of the actual group.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

215
08:56.960 --> 09:00.870
I'll have let's say a hyphen and then just give the name of the actual group.

216
09:00.870 --> 09:01.520
So I'll have my double

217
09:01.520 --> 09:04.880
color brace and then give the variable name group. Once again choose any name

218
09:04.880 --> 09:05.920
you so wish for this

219
09:05.920 --> 09:09.790
group just happens to be descriptive now because we have opened our four

220
09:09.790 --> 09:11.440
statements here. This is

221
09:11.440 --> 09:15.700
actually now being closed off with this particular four statement the next one

222
09:15.700 --> 09:16.800
to it the one below

223
09:16.800 --> 09:20.500
but we still have this particular four statement opened up and that means we're

224
09:20.500 --> 09:21.360
going to have to

225
09:21.360 --> 09:25.080
close it again so it's going to be closed at the very end. So we just say in

226
09:25.080 --> 09:26.400
four percentage

227
09:26.400 --> 09:31.370
and color brace if I now escape writing quits clear the screen say answerable
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

227
09:26.400 --> 09:31.370
and color brace if I now escape writing quits clear the screen say answerable

228
09:31.370 --> 09:32.480
hyphen playbook

229
09:32.480 --> 09:36.620
render test at YML hit enter and we can see from the recap we have updated the

230
09:36.620 --> 09:37.040
files.

231
09:37.040 --> 09:41.210
Let's go to the yellow box here let's cut the file if I hit enter look at this

232
09:41.210 --> 09:41.920
right here.

233
09:41.920 --> 09:45.520
Now we have the original text followed by on my system I have the following

234
09:45.520 --> 09:46.000
users

235
09:46.000 --> 09:48.820
answerable and then the groups here and then we have iterated through all of

236
09:48.820 --> 09:49.680
the groups

237
09:49.680 --> 09:53.140
then we have nugget user iterated through all the nugget user groups and then n

238
09:53.140 --> 09:54.240
ox we have iterated

239
09:54.240 --> 09:58.560
through all of noxes groups now if we go on to the other machine we should see

240
09:58.560 --> 09:59.200
something very

241
09:59.200 --> 10:03.310
similar but this time actually we should see the James user in amongst the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

241
09:59.200 --> 10:03.310
similar but this time actually we should see the James user in amongst the

242
10:03.310 --> 10:04.000
configuration.

243
10:04.000 --> 10:07.560
So let's cut the file as we can see here there we go right there we have answer

244
10:07.560 --> 10:08.160
able with the

245
10:08.160 --> 10:12.380
answerable groups nugget user with all of their groups and James with all of

246
10:12.380 --> 10:13.280
James's groups.

247
10:13.280 --> 10:16.690
So that is how we can utilize groups within ginger tool like I say if this is

248
10:16.690 --> 10:18.080
your first exposure

249
10:18.080 --> 10:22.080
this can be a little bit overwhelming the key here as always is just a little

250
10:22.080 --> 10:22.880
bit of practice a

251
10:22.880 --> 10:26.170
little bit of familiarity but now that we've got that under a belt the next

252
10:26.170 --> 10:26.960
thing I want to talk

253
10:26.960 --> 10:31.110
to you about is how we can use conditional logic again this time within our
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

253
10:26.960 --> 10:31.110
to you about is how we can use conditional logic again this time within our

254
10:31.110 --> 10:32.480
ginger tool templates.
