WEBVTT

1
00:00.000 --> 00:03.320
Okay friends, so let's talk about YAML.

2
00:03.320 --> 00:06.520
Now all YAML is is a data serialization language.

3
00:06.520 --> 00:09.200
And the big thing about this particular file format

4
00:09.200 --> 00:12.080
is that we have an emphasis on human readability.

5
00:12.080 --> 00:15.280
So maybe perhaps you are familiar with other file formats

6
00:15.280 --> 00:18.680
such as JSON or perhaps even XML.

7
00:18.680 --> 00:22.080
And ultimately they allow us to do a similar thing with data.

8
00:22.080 --> 00:23.960
The data is just presented a different way

9
00:23.960 --> 00:26.220
and each of these have their own rule sets.

10
00:26.220 --> 00:29.280
So how about we discuss some of the rules behind YAML

11
00:29.280 --> 00:30.960
as well as see some examples
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:29.280 --> 00:30.960
as well as see some examples

12
00:30.960 --> 00:33.680
so that we can actually see what this really looks like.

13
00:33.680 --> 00:35.480
Okay, so the very first thing to say

14
00:35.480 --> 00:37.880
and this is very, very important to understand

15
00:37.880 --> 00:40.840
is that YAML is a white space sensitive.

16
00:40.840 --> 00:43.040
Reason being is that with respect to YAML

17
00:43.040 --> 00:47.140
we have a very big emphasis on the concept of indentation.

18
00:47.140 --> 00:49.200
So when we begin to explore some examples

19
00:49.200 --> 00:52.120
you're going to see that we can actually associate

20
00:52.120 --> 00:55.440
particular elements within a YAML document together

21
00:55.440 --> 00:57.160
using this concept of indentation.

22
00:57.160 --> 00:59.640
And if you get the indentation wrong

23
00:59.640 --> 01:02.240
the association between different elements
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:59.640 --> 01:02.240
the association between different elements

24
01:02.240 --> 01:04.360
can become confused and in some cases

25
01:04.360 --> 01:06.320
just become completely invalid.

26
01:06.320 --> 01:08.400
So really, really, really be aware

27
01:08.400 --> 01:10.600
of the type of indentation that you use.

28
01:10.600 --> 01:12.200
Now with respect to YAML

29
01:12.200 --> 01:15.520
you can use four spaces indentation.

30
01:15.520 --> 01:17.240
This is fairly commonly used

31
01:17.240 --> 01:19.800
but the general convention the most common way

32
01:19.800 --> 01:22.000
is in fact to use two spaces.

33
01:22.000 --> 01:23.760
Now there is no hard and fast rule

34
01:23.760 --> 01:25.520
that you have to use one or the other.

35
01:25.520 --> 01:27.400
These are the more common implementations

36
01:27.400 --> 01:30.200
like I say with two spaces being the most popular.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

36
01:27.400 --> 01:30.200
like I say with two spaces being the most popular.

37
01:30.200 --> 01:32.320
As such because of this convention

38
01:32.320 --> 01:34.640
I myself prefer to use two spaces

39
01:34.640 --> 01:36.760
that just keeps things a little bit neater.

40
01:36.760 --> 01:38.600
So in my particular examples

41
01:38.600 --> 01:41.160
that is the convention I'm going to follow.

42
01:41.160 --> 01:42.400
Now whilst it's not important

43
01:42.400 --> 01:45.880
if you happen to use two or four spaces indentation

44
01:45.880 --> 01:47.360
the main thing we want to remember

45
01:47.360 --> 01:49.560
is that we do not want to mix and match them.

46
01:49.560 --> 01:52.320
You know use four spaces for one part of the document

47
01:52.320 --> 01:53.920
and then suddenly start using two.

48
01:53.920 --> 01:56.400
This is absolutely not what we want to do.

49
01:56.400 --> 01:57.920
Basically pick something that you like

50
01:57.920 --> 02:00.080
and just stick with it.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

50
01:57.920 --> 02:00.080
and just stick with it.

51
02:00.080 --> 02:02.640
Now also to be clear you may have the temptation

52
02:02.640 --> 02:05.080
to use tabs for your indentation

53
02:05.080 --> 02:07.400
but realistically the rules of YAML specify

54
02:07.400 --> 02:09.640
that you should be using spaces instead

55
02:09.640 --> 02:11.600
so actually use that space bar.

56
02:11.600 --> 02:15.000
Now if you happen to be absolutely adamant on using tabs

57
02:15.000 --> 02:17.840
you just find it quicker to get that indentation.

58
02:17.840 --> 02:19.680
You can still use a tab key

59
02:19.680 --> 02:22.640
but what I suggest you do is you configure your text editor

60
02:22.640 --> 02:26.520
to actually convert the tabs into spaces that way

61
02:26.520 --> 02:29.240
even if you press the tab to get quick indentation

62
02:29.240 --> 02:31.160
ultimately under the hood it's going to be translated
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

62
02:29.240 --> 02:31.160
ultimately under the hood it's going to be translated

63
02:31.160 --> 02:32.520
and converted into spaces

64
02:32.520 --> 02:34.240
that will keep things nice and consistent

65
02:34.240 --> 02:36.200
with the expected rules of YAML.

66
02:36.200 --> 02:38.120
Now another thing to note in YAML

67
02:38.120 --> 02:40.800
is that we happen to denote the start of a YAML document

68
02:40.800 --> 02:42.200
with three hyphens.

69
02:42.200 --> 02:45.040
So when we happen to be writing our Ansible Playbooks

70
02:45.040 --> 02:46.440
this is what you can expect to see

71
02:46.440 --> 02:48.720
at the beginning of a particular play.

72
02:48.720 --> 02:50.880
Just to be clear though this is not actually

73
02:50.880 --> 02:53.320
an enforced convention you can absolutely create

74
02:53.320 --> 02:55.840
a YAML document without these three hyphens.

75
02:55.840 --> 02:57.520
This is just a general standard

76
02:57.520 --> 02:59.560
used to denote the start of a YAML document.

77
02:59.560 --> 03:02.880
In fact if you want to have multiple YAML documents
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

77
02:59.560 --> 03:02.880
In fact if you want to have multiple YAML documents

78
03:02.880 --> 03:04.760
within the same file so to speak

79
03:04.760 --> 03:06.320
you can break up that document

80
03:06.320 --> 03:09.320
ultimately creating these new YAML Paragraphs so to speak

81
03:09.320 --> 03:11.240
by using these three hyphens

82
03:11.240 --> 03:13.200
that is going to separate them out.

83
03:13.200 --> 03:15.120
Now another big advantage of YAML

84
03:15.120 --> 03:17.400
and one of the reasons why it's very very popular

85
03:17.400 --> 03:20.200
for configuration files is that allows us to use

86
03:20.200 --> 03:21.480
this hash symbol.

87
03:21.480 --> 03:22.920
Now the use of the hash symbol itself

88
03:22.920 --> 03:24.640
isn't actually a revelation.

89
03:24.640 --> 03:26.640
Rather what I'm talking about is what this hash symbol

90
03:26.640 --> 03:29.360
allows us to do, it allows us to create comments.

91
03:29.360 --> 03:32.200
Now if you are familiar with the JSON format
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

91
03:29.360 --> 03:32.200
Now if you are familiar with the JSON format

92
03:32.200 --> 03:34.280
JSON is rather human readable

93
03:34.280 --> 03:36.120
but one of the big downfalls within JSON

94
03:36.120 --> 03:38.400
is that you do not have the ability for comments.

95
03:38.400 --> 03:40.040
Now one of YAML's primary use cases

96
03:40.040 --> 03:41.680
is for creating configuration files

97
03:41.680 --> 03:44.520
having the ability to pair a configuration

98
03:44.520 --> 03:46.560
with a particular comment beside it

99
03:46.560 --> 03:48.520
giving the reader some type of context

100
03:48.520 --> 03:51.760
about what this configuration is actually supposed to do.

101
03:51.760 --> 03:55.200
This is one of the big ones that we get with the YAML format.

102
03:55.200 --> 03:56.760
Another thing that we should be aware of

103
03:56.760 --> 03:59.480
is that YAML files are going to end in the extension

104
03:59.480 --> 04:02.960
either .YML or .YAML.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

104
03:59.480 --> 04:02.960
either .YML or .YAML.

105
04:02.960 --> 04:06.160
Now generally speaking the more common use

106
04:06.160 --> 04:07.960
is to just say .YAML.

107
04:07.960 --> 04:11.400
However for whatever reason and the Ansible Ecosystems fear

108
04:11.400 --> 04:14.280
we tend to use .YML so as such

109
04:14.280 --> 04:15.880
this is going to be the convention

110
04:15.880 --> 04:18.480
that I'm going to follow within this particular course.

111
04:18.480 --> 04:20.400
Just be aware that this is not mandatory

112
04:20.400 --> 04:22.880
if you do see .YAML, do not freak out

113
04:22.880 --> 04:25.440
it still is absolutely fine and accepted.

114
04:25.440 --> 04:28.320
So let's take a look at an example YAML documents.

115
04:28.320 --> 04:31.760
So here's a tip, when working with YAML
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

115
04:28.320 --> 04:31.760
So here's a tip, when working with YAML

116
04:31.760 --> 04:34.000
one of the best things to do is to make use

117
04:34.000 --> 04:36.120
of an online YAML parser.

118
04:36.120 --> 04:37.400
Pretty much what these will do

119
04:37.400 --> 04:40.720
is they will programmatically assess your YAML documents

120
04:40.720 --> 04:42.400
and if you have any type of errors

121
04:42.400 --> 04:44.000
perhaps you have an indentation error

122
04:44.000 --> 04:46.640
perhaps you're using some type of invalid character.

123
04:46.640 --> 04:50.120
Whatever it is, this particular parser here

124
04:50.120 --> 04:52.040
or any parser online for example

125
04:52.040 --> 04:53.960
will be able to detect that problem

126
04:53.960 --> 04:55.480
and point out how to remedy it.

127
04:55.480 --> 04:57.040
So really when you're writing playbooks

128
04:57.040 --> 04:59.160
this can save you quite a lot of time.

129
04:59.160 --> 05:01.040
So if we just collect one of these parsers
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

129
04:59.160 --> 05:01.040
So if we just collect one of these parsers

130
05:01.040 --> 05:03.640
let's maybe go with YAML parser online right here

131
05:03.640 --> 05:04.920
on code beautify.

132
05:04.920 --> 05:06.280
So on the left hand side here

133
05:06.280 --> 05:09.720
what I've done is pasted in a very simple YAML document

134
05:09.720 --> 05:10.560
that I created.

135
05:10.560 --> 05:12.120
Notice at the very top of the document

136
05:12.120 --> 05:13.840
we have our three hyphened right here

137
05:13.840 --> 05:16.760
the noting that this is the beginning of a new YAML doc.

138
05:16.760 --> 05:18.440
And then we have some information

139
05:18.440 --> 05:20.760
to see the name and the corresponding value

140
05:20.760 --> 05:22.000
as John McGovern.

141
05:22.000 --> 05:25.560
We have an age of 99 which is not my actual age

142
05:25.560 --> 05:28.200
thankfully and we have an email

143
05:28.200 --> 05:30.960
and the corresponding value we have UK citizen
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
05:28.200 --> 05:30.960
and the corresponding value we have UK citizen

144
05:30.960 --> 05:32.680
and then we have something called true.

145
05:32.680 --> 05:35.280
This happens to be known as a billion value.

146
05:35.280 --> 05:37.080
This is supported in YAML

147
05:37.080 --> 05:38.800
which pretty much allows us to have this type

148
05:38.800 --> 05:40.920
of binary logic as something true

149
05:40.920 --> 05:43.200
or as something false we'll get to see more of this

150
05:43.200 --> 05:44.800
later on throughout this course.

151
05:44.800 --> 05:46.760
Just to understand that we can have a regular text

152
05:46.760 --> 05:48.600
or sentences so to speak.

153
05:48.600 --> 05:50.360
These are known as strings.

154
05:50.360 --> 05:52.640
We can actually have numerical values here

155
05:52.640 --> 05:54.520
which would be known as integers.

156
05:54.520 --> 05:55.600
These are your numbers.

157
05:55.600 --> 05:57.600
And we have these billion values here.

158
05:57.600 --> 05:59.120
Now there actually are more data types

159
05:59.120 --> 06:00.600
that we can specify within YAML.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

159
05:59.120 --> 06:00.600
that we can specify within YAML.

160
06:00.600 --> 06:03.080
We'll talk about more of that in an upcoming video.

161
06:03.080 --> 06:04.480
And we also have some information

162
06:04.480 --> 06:06.320
about what skills I can do.

163
06:06.320 --> 06:08.800
And really this is what a very simple YAML document

164
06:08.800 --> 06:09.640
looks like.

165
06:09.640 --> 06:11.720
Now of course as the data becomes more complex,

166
06:11.720 --> 06:13.240
becomes more populated,

167
06:13.240 --> 06:16.320
things can be a little bit more complicated to look at.

168
06:16.320 --> 06:18.040
But really so long as you understand

169
06:18.040 --> 06:20.080
the actual rows of YAML,

170
06:20.080 --> 06:23.000
you can map out all this data programmatically.

171
06:23.000 --> 06:24.840
So this could be a file for the configurations

172
06:24.840 --> 06:26.880
that you want to have across all of your servers

173
06:26.880 --> 06:27.760
within a network.

174
06:27.760 --> 06:29.240
By following the rows of YAML,

175
06:29.240 --> 06:30.680
we can have this information,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

175
06:29.240 --> 06:30.680
we can have this information,

176
06:30.680 --> 06:33.160
any standardized and programmatic way

177
06:33.160 --> 06:34.520
that is consistent.

178
06:34.520 --> 06:35.720
And as we can see here,

179
06:35.720 --> 06:37.880
fairly easy to read for a human.

180
06:37.880 --> 06:39.200
You may not be great at programming

181
06:39.200 --> 06:40.600
but I'm pretty sure you can understand

182
06:40.600 --> 06:42.720
and interpret what we're actually seeing here

183
06:42.720 --> 06:43.760
on the left hand side.

184
06:43.760 --> 06:45.280
Now the cool thing like I said about things

185
06:45.280 --> 06:46.840
like these YAML parsers,

186
06:46.840 --> 06:49.440
is there allows us to see where you may have went wrong.

187
06:49.440 --> 06:50.600
So say for example,

188
06:50.600 --> 06:52.360
if I remove the three hyphens,

189
06:52.360 --> 06:56.000
notice that the YAML document is still actually valid.

190
06:56.000 --> 06:58.240
Well, they may be visually instructive.

191
06:58.240 --> 06:59.520
They're not actually necessary

192
06:59.520 --> 07:01.080
to construct the YAML documents.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

192
06:59.520 --> 07:01.080
to construct the YAML documents.

193
07:01.080 --> 07:03.360
However, what if I happen to do something like say,

194
07:03.360 --> 07:04.680
this in the document,

195
07:04.680 --> 07:06.120
notice on the left hand side,

196
07:06.120 --> 07:07.800
we have this little X telling us

197
07:07.800 --> 07:09.320
that we have some type of error.

198
07:09.320 --> 07:11.200
And it tells us that we have now introduced

199
07:11.200 --> 07:13.280
an unknown tag into the document.

200
07:13.280 --> 07:15.480
And we can see here on the right hand side,

201
07:15.480 --> 07:17.880
the rendered value is now simply null,

202
07:17.880 --> 07:20.440
as opposed to the value of the name.

203
07:20.440 --> 07:22.400
Similarly, if I just happened to start typing

204
07:22.400 --> 07:25.040
in some absolute garbage into the document right here,

205
07:25.040 --> 07:26.480
look at this on the right hand side,

206
07:26.480 --> 07:28.280
we can clearly see that we have an error.

207
07:28.280 --> 07:30.600
This is not valid YAML at all.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

207
07:28.280 --> 07:30.600
This is not valid YAML at all.

208
07:30.600 --> 07:32.160
And we can actually see here,

209
07:32.160 --> 07:34.400
the line number where the problem is introduced,

210
07:34.400 --> 07:35.240
line number three.

211
07:35.240 --> 07:36.880
So we know starting right here,

212
07:36.880 --> 07:39.440
this is where the problem begins, so if we remove this,

213
07:39.440 --> 07:41.440
we can see we still have a problem on line four.

214
07:41.440 --> 07:42.600
So we remove line four,

215
07:42.600 --> 07:44.360
we see we have a problem on line five,

216
07:44.360 --> 07:46.800
if we remove line five and hit enter,

217
07:46.800 --> 07:48.680
suddenly that problem has been fixed.

218
07:48.680 --> 07:51.200
And if we resolve the problem here of the unknown tag,

219
07:51.200 --> 07:53.080
remove that, suddenly once again,

220
07:53.080 --> 07:55.240
the YAML document is valid.

221
07:55.240 --> 07:56.840
Definitely, I would encourage you to make use

222
07:56.840 --> 07:58.000
of these YAML tools.

223
07:58.000 --> 07:59.760
It's going to make your life much, much easier

224
07:59.760 --> 08:02.360
because whilst YAML is very human readable,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

224
07:59.760 --> 08:02.360
because whilst YAML is very human readable,

225
08:02.360 --> 08:04.800
it is notoriously easy to make a mistake

226
08:04.800 --> 08:05.720
in a YAML document

227
08:05.720 --> 08:08.360
because of the whites based sensitivity

228
08:08.360 --> 08:10.880
and the emphasis on indentation levels.
