WEBVTT

1
00:00.000 --> 00:03.000
So friends, now that we have our variable files defined,

2
00:03.000 --> 00:06.300
how about we begin writing our first Ginger templates.

3
00:06.300 --> 00:08.600
So with that said, let's just get to it.

4
00:08.600 --> 00:10.280
Okay, so the first thing that I'm going to do

5
00:10.280 --> 00:12.760
is I'm going to go into my main directory right here

6
00:12.760 --> 00:15.480
and I'm going to create a new directory

7
00:15.480 --> 00:18.400
simply to hold my template to files.

8
00:18.400 --> 00:20.360
So I'll just say mkdir and call this templates.

9
00:20.360 --> 00:22.480
Again, call this anything you so wish.

10
00:22.480 --> 00:25.240
Just be aware that whatever you name here,

11
00:25.240 --> 00:26.500
you're going to have to reference

12
00:26.500 --> 00:28.060
within the playbook itself.

13
00:28.060 --> 00:30.220
So inside of this directory right now,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

13
00:28.060 --> 00:30.220
So inside of this directory right now,

14
00:30.220 --> 00:32.140
all I'm going to do is I'm going to create a file

15
00:32.140 --> 00:33.980
or rather a template file.

16
00:33.980 --> 00:37.140
And let me just call this test.j2.

17
00:37.140 --> 00:39.460
Okay, the name itself doesn't actually matter.

18
00:39.460 --> 00:42.540
I would just recommend sticking with that.j2 extension.

19
00:42.540 --> 00:45.700
So let's go into this particular file right here.

20
00:45.700 --> 00:49.020
Now here is the thing, with respect to Ginger,

21
00:49.020 --> 00:51.980
we can just begin writing text as we ordinarily would.

22
00:51.980 --> 00:54.180
Say for example, I wanted to do this, okay.

23
00:54.180 --> 00:58.300
This is an example of a Ginger template.

24
00:58.300 --> 01:02.460
So this actually is a valid Ginger 2 template file
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

24
00:58.300 --> 01:02.460
So this actually is a valid Ginger 2 template file

25
01:02.460 --> 01:03.460
as it is right now.

26
01:03.460 --> 01:05.740
It's just not a very useful one.

27
01:05.740 --> 01:08.180
We're not doing any type of variable substitution

28
01:08.180 --> 01:10.780
or using any of the programmatic logic

29
01:10.780 --> 01:13.780
that we can ordinarily use when working with Ginger.

30
01:13.780 --> 01:15.780
So let's spice it up a little bit.

31
01:15.780 --> 01:20.060
Let's add in my name is, and at this point here,

32
01:20.060 --> 01:21.780
what we're going to do is we're going to use

33
01:21.780 --> 01:24.260
our variable substitution.

34
01:24.260 --> 01:26.900
Now similar to what we saw before within our Playbooks,

35
01:26.900 --> 01:29.060
we can call these variables utilizing

36
01:29.060 --> 01:30.700
double curly braces right here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

36
01:29.060 --> 01:30.700
double curly braces right here.

37
01:30.700 --> 01:32.540
Now what exactly is the name of the variable

38
01:32.540 --> 01:33.540
that I'm going to call?

39
01:33.540 --> 01:36.980
Well, if we go and reference the vars, for example,

40
01:36.980 --> 01:38.100
let's look at another tab.

41
01:38.100 --> 01:39.780
So let me cat the hostfars,

42
01:39.780 --> 01:41.740
and let's go with blue box.yaml first.

43
01:41.740 --> 01:43.740
If we want to get the name, what do we do?

44
01:43.740 --> 01:46.700
We're going to have to call the device roll key.

45
01:46.700 --> 01:48.540
And then to get the name itself,

46
01:48.540 --> 01:51.740
we will use dot notation similar to what we saw before,

47
01:51.740 --> 01:53.300
to call the name key.

48
01:53.300 --> 01:55.460
So that's exactly what we're going to do, device roll,

49
01:55.460 --> 01:56.500
then name.

50
01:56.500 --> 01:59.540
So we'll just say device underscore roll,

51
01:59.540 --> 02:00.900
and then dot name.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

51
01:59.540 --> 02:00.900
and then dot name.

52
02:00.900 --> 02:02.820
Let's look at what other information we have.

53
02:02.820 --> 02:06.340
We have a type, so to say my roll in the network

54
02:06.340 --> 02:09.540
is as a, and then I'll have my variable substitution,

55
02:09.540 --> 02:11.300
once again, we'll call device roll,

56
02:11.300 --> 02:13.300
and then we'll grab the type key.

57
02:13.300 --> 02:15.700
Perfect. And if we happen to go to say, for example,

58
02:15.700 --> 02:17.780
the group vars and go to the all dot yaml,

59
02:17.780 --> 02:20.460
we can see the domain info, we have CBT targets land,

60
02:20.460 --> 02:22.420
and the location is Oregon.

61
02:22.420 --> 02:24.060
Now just to highlight the spacing here,

62
02:24.060 --> 02:26.460
if I wanted to have some type of particular formatting,

63
02:26.460 --> 02:28.260
whereby we needed to have indentation

64
02:28.260 --> 02:30.460
in our configuration file, whatever it may be,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

64
02:28.260 --> 02:30.460
in our configuration file, whatever it may be,

65
02:30.460 --> 02:32.060
we can add these configurations,

66
02:32.060 --> 02:34.100
or rather add these indentations

67
02:34.100 --> 02:35.780
to our configurations, no problem at all.

68
02:35.780 --> 02:37.500
We could just do, say, for example,

69
02:37.500 --> 02:39.580
one, two, three, four spaces,

70
02:39.580 --> 02:41.380
and just say I live in,

71
02:41.380 --> 02:44.380
and then call my variable substitution, once again.

72
02:44.380 --> 02:45.420
Let's look at the actual path.

73
02:45.420 --> 02:47.340
We're going to have to have domain underscore info,

74
02:47.340 --> 02:48.300
and then the name.

75
02:48.300 --> 02:50.540
So just put that within our double color braces,

76
02:50.540 --> 02:53.260
which is in the state of,

77
02:53.260 --> 02:55.140
and again, domain underscore name,

78
02:55.140 --> 02:56.940
and to get the actual location here,

79
02:56.940 --> 02:58.420
we call the location key.

80
02:58.420 --> 02:59.460
Now just to point out,

81
02:59.460 --> 03:01.100
the actual example here that we're doing here
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

81
02:59.460 --> 03:01.100
the actual example here that we're doing here

82
03:01.100 --> 03:03.900
is not exactly the most useful one, as you can imagine.

83
03:03.900 --> 03:06.580
This is obviously not an actual Linux configuration.

84
03:06.580 --> 03:09.860
And in fact, as it transpires with this config here,

85
03:09.860 --> 03:11.580
and this config here,

86
03:11.580 --> 03:13.460
because our network is so small,

87
03:13.460 --> 03:15.300
we only have two devices,

88
03:15.300 --> 03:17.220
and these are applied to the all group.

89
03:17.220 --> 03:19.780
Realistically, we could just hard code these values

90
03:19.780 --> 03:21.820
directly within the template itself.

91
03:21.820 --> 03:24.260
But despite this, even though we could do this,

92
03:24.260 --> 03:25.580
it still has kind of a good idea

93
03:25.580 --> 03:27.300
to have this templatization on the go,

94
03:27.300 --> 03:30.580
because what it means is if you want to update
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

94
03:27.300 --> 03:30.580
because what it means is if you want to update

95
03:30.580 --> 03:32.540
the actual variable files,

96
03:32.540 --> 03:34.820
say, for example, the location happened to change,

97
03:34.820 --> 03:37.300
we don't have to go into perhaps a very long

98
03:37.300 --> 03:40.380
and complex Ginger2 configuration file.

99
03:40.380 --> 03:42.940
We can just quickly modify these much more readable

100
03:42.940 --> 03:46.100
and manageable and smaller YAML files.

101
03:46.100 --> 03:48.500
So just giving you some additional context here.

102
03:48.500 --> 03:51.980
So let's just do escape colon wq to write and quits.

103
03:51.980 --> 03:53.900
And what I will now do is I will go and create

104
03:53.900 --> 03:56.300
a little playbook that is going to effectively

105
03:56.300 --> 03:57.860
render these configurations.

106
03:57.860 --> 03:59.260
So let's just say touch,

107
03:59.260 --> 04:02.260
and let's just call this render test dot yml,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

107
03:59.260 --> 04:02.260
and let's just call this render test dot yml,

108
04:02.260 --> 04:04.580
and we'll use them to edit this particular file.

109
04:04.580 --> 04:06.500
We'll do insert mode, we'll have our three hivens

110
04:06.500 --> 04:09.380
at the top, we'll give our name of the player.

111
04:09.380 --> 04:11.220
So we have the name key, and we'll just add in

112
04:11.220 --> 04:14.420
testing our Ginger2 templates.

113
04:14.420 --> 04:16.900
Now, who are we actually acting on, but the host key?

114
04:16.900 --> 04:18.740
The notes we're going to act on the all group.

115
04:18.740 --> 04:21.260
Now, to specify the tasks that I want to create,

116
04:21.260 --> 04:23.580
we'll give the task a name, just say render,

117
04:23.580 --> 04:26.660
config, file, and push to target.

118
04:26.660 --> 04:28.660
Now, at this point here, what we're going to do

119
04:28.660 --> 04:30.740
is to use that module that we talked about
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

119
04:28.660 --> 04:30.740
is to use that module that we talked about

120
04:30.740 --> 04:33.740
in a previous video, that was the Ansible built-in

121
04:33.740 --> 04:34.820
template module.

122
04:34.820 --> 04:38.100
So I'll just say Ansible dot built-in dot template.

123
04:38.100 --> 04:40.380
Now, like I say, we can actually shorthand this

124
04:40.380 --> 04:41.580
just to have the word template,

125
04:41.580 --> 04:42.900
but I like to be explicit here,

126
04:42.900 --> 04:45.820
just say any type of ambiguity or confusion.

127
04:45.820 --> 04:48.340
And I've just realized I have a syntax error

128
04:48.340 --> 04:51.060
that should not actually be an array, let me fix this.

129
04:51.060 --> 04:53.340
And as such, let me fix the indentation here,

130
04:53.340 --> 04:55.580
move this out, and similarly here.

131
04:55.580 --> 04:58.380
So recall with respect to the template module,

132
04:58.380 --> 05:01.420
we have two parameters that are absolutely mandatory.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

132
04:58.380 --> 05:01.420
we have two parameters that are absolutely mandatory.

133
05:01.420 --> 05:03.540
We're going to have to have the source parameter,

134
05:03.540 --> 05:06.820
which is SRC, as well as the destination parameter,

135
05:06.820 --> 05:07.660
dest.

136
05:07.660 --> 05:10.140
Now, the source parameter is pretty much where our file

137
05:10.140 --> 05:13.020
happens to reside, or rather our configuration template

138
05:13.020 --> 05:14.100
happens to reside.

139
05:14.100 --> 05:16.580
Now, recall what happens to be in the templates directory.

140
05:16.580 --> 05:19.020
Now, the thing about Ansible is that by default,

141
05:19.020 --> 05:21.380
it's going to automatically seek to look

142
05:21.380 --> 05:23.180
for the templates directory.

143
05:23.180 --> 05:24.300
That is the default behavior.

144
05:24.300 --> 05:26.900
So because of that, all I'm going to do here

145
05:26.900 --> 05:30.700
is specify that the file should just be called test.jtubic,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

145
05:26.900 --> 05:30.700
is specify that the file should just be called test.jtubic,

146
05:30.700 --> 05:32.700
because like I say, Ansible is going to seek to find

147
05:32.700 --> 05:35.420
the templates folder and search for the contents

148
05:35.420 --> 05:36.540
of it automatically.

149
05:36.540 --> 05:38.260
Now, with respect to the destination,

150
05:38.260 --> 05:41.260
this is really where about on the target device,

151
05:41.260 --> 05:42.900
in this case here, we have all group,

152
05:42.900 --> 05:45.740
which comprises of the yellow box and blue box,

153
05:45.740 --> 05:49.860
where on those devices do we actually want to write

154
05:49.860 --> 05:51.020
the rendered file?

155
05:51.020 --> 05:52.940
So let's just maybe say we want to target

156
05:52.940 --> 05:55.260
the temp directory within each device,

157
05:55.260 --> 05:56.900
and let's just call the file, oh, I don't know,

158
05:56.900 --> 05:59.940
let's say config test.txt, okay?

159
05:59.940 --> 06:04.220
So let's press escape, colon wq2, write an quit.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

159
05:59.940 --> 06:04.220
So let's press escape, colon wq2, write an quit.

160
06:04.220 --> 06:06.100
And I'll just say Ansible Playbook,

161
06:06.100 --> 06:09.260
render test.yml if I hit enter here.

162
06:09.260 --> 06:11.660
Oh, and as we can see here, we actually have an error

163
06:11.660 --> 06:14.180
that says Ansible undefined variable.

164
06:14.180 --> 06:17.740
And it tells us that the domain_name is undefined.

165
06:17.740 --> 06:20.660
Now the problem is, is because in the actual template,

166
06:20.660 --> 06:23.100
I reference this as domain_name,

167
06:23.100 --> 06:24.380
and if we scroll on down here,

168
06:24.380 --> 06:28.420
look at the actual file, it should be domain_info.

169
06:28.420 --> 06:29.740
So there is a good learning example,

170
06:29.740 --> 06:31.820
be sure to reference the correct keys
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

170
06:29.740 --> 06:31.820
be sure to reference the correct keys

171
06:31.820 --> 06:33.140
within your variable files.

172
06:33.140 --> 06:35.420
So let's go quickly back into the templates,

173
06:35.420 --> 06:37.140
and we're just going to insert modes.

174
06:37.140 --> 06:38.240
And I'll just change this one here,

175
06:38.240 --> 06:40.260
notice I actually got it correct,

176
06:40.260 --> 06:42.140
the first time domain_info,

177
06:42.140 --> 06:43.140
and for the second time,

178
06:43.140 --> 06:45.540
I don't know why I wrote domain_name.

179
06:45.540 --> 06:48.300
You at home were probably shouting at the screen

180
06:48.300 --> 06:50.540
when I made that mistake, so apologies there.

181
06:50.540 --> 06:51.500
Let's try this again.

182
06:51.500 --> 06:53.340
Once again, always good to see when Ansible,

183
06:53.340 --> 06:54.180
Spitzer and error,

184
06:54.180 --> 06:55.900
and how we can actually handle that error,

185
06:55.900 --> 06:57.020
it's quite instructive.

186
06:57.020 --> 06:59.500
So let's try it again, one more time, hit enter.

187
06:59.500 --> 07:00.500
And as we can see here,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

187
06:59.500 --> 07:00.500
And as we can see here,

188
07:00.500 --> 07:03.140
we have a change on both devices,

189
07:03.140 --> 07:04.860
with nothing unreachable, nothing failed,

190
07:04.860 --> 07:06.020
and nothing skipped.

191
07:06.020 --> 07:09.220
So what we want to do is to go to both of those devices,

192
07:09.220 --> 07:12.380
and to actually see what the result happens to be.

193
07:12.380 --> 07:13.780
So if we go into the 10th directory,

194
07:13.780 --> 07:16.060
do an LS, and if I let's do an LS,

195
07:16.060 --> 07:17.740
and great for config,

196
07:17.740 --> 07:19.260
there is the actual file right here,

197
07:19.260 --> 07:20.740
let's just cat this.

198
07:20.740 --> 07:22.060
And look at this right here,

199
07:22.060 --> 07:23.180
notice by the way,

200
07:23.180 --> 07:25.900
the actual indentation has been maintained.

201
07:25.900 --> 07:26.900
We can see here,

202
07:26.900 --> 07:28.860
this is an example of a Ginger template,

203
07:28.860 --> 07:30.780
which was the original line I wrote.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

203
07:28.860 --> 07:30.780
which was the original line I wrote.

204
07:30.780 --> 07:31.900
And then we have the line,

205
07:31.900 --> 07:33.820
which has the variable substitution.

206
07:33.820 --> 07:35.660
My name is yellowDB,

207
07:35.660 --> 07:38.580
my role in the network is as a database,

208
07:38.580 --> 07:40.060
and indentations in i,

209
07:40.060 --> 07:41.460
live in CBT and nuggets land,

210
07:41.460 --> 07:43.060
which is in the state of Oregon.

211
07:43.060 --> 07:43.900
Perfect.

212
07:43.900 --> 07:46.300
Let's go and check the render configuration,

213
07:46.300 --> 07:47.980
and the other device.

214
07:47.980 --> 07:49.660
So going to the 10th directory,

215
07:49.660 --> 07:50.500
do an LS,

216
07:50.500 --> 07:52.380
and great for config, once again,

217
07:52.380 --> 07:54.260
we can see that we have this particular file,

218
07:54.260 --> 07:55.460
if we just cat it,

219
07:55.460 --> 07:56.940
we can see the same type of thing,

220
07:56.940 --> 07:58.620
this is an example of a Ginger template.

221
07:58.620 --> 07:59.740
This time though,

222
07:59.740 --> 08:01.420
we have the correct variable substitution,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

222
07:59.740 --> 08:01.420
we have the correct variable substitution,

223
08:01.420 --> 08:03.700
because we now say my name is blue box,

224
08:03.700 --> 08:06.740
web, my role in the network is as a web server,

225
08:06.740 --> 08:08.860
once again maintaining indentation,

226
08:08.860 --> 08:10.860
i, live in CBT nuggets land,

227
08:10.860 --> 08:12.620
which is in the state of Oregon.

228
08:12.620 --> 08:15.220
So we can clearly see here that by utilizing

229
08:15.220 --> 08:16.540
the template module,

230
08:16.540 --> 08:18.580
we can reference these Ginger two templates,

231
08:18.580 --> 08:21.620
and populate in our variable values,

232
08:21.620 --> 08:23.980
so that we are able to create different configurations

233
08:23.980 --> 08:26.580
for different devices within our network.
