WEBVTT

1
00:00.000 --> 00:05.040
So friends, it is now time to talk about the elusive Ansible Playbook. So the

2
00:05.040 --> 00:05.680
first thing that

3
00:05.680 --> 00:09.090
I'm going to recommend you do is to go to the Ansible documentation. So we

4
00:09.090 --> 00:10.080
search for Ansible

5
00:10.080 --> 00:14.420
Playbooks if you click the top link right here. So notice that it says that the

6
00:14.420 --> 00:15.520
Ansible Playbook

7
00:15.520 --> 00:20.480
allows for repeatable reusable and simple configuration management. This says

8
00:20.480 --> 00:20.720
really

9
00:20.720 --> 00:25.420
quick contrast from the ad hoc commands that were really just used for a one-

10
00:25.420 --> 00:26.240
off purpose.

11
00:26.240 --> 00:30.230
This time here, we are focused on automation that we expect to be repeatable
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:26.240 --> 00:30.230
This time here, we are focused on automation that we expect to be repeatable

12
00:30.230 --> 00:31.360
and reusable. So if

13
00:31.360 --> 00:35.090
you want to maybe update your servers once a week, that type of thing, we want

14
00:35.090 --> 00:36.000
to have all of this

15
00:36.000 --> 00:40.940
declared within a playbook. So if we look at the playbook syntax right here, no

16
00:40.940 --> 00:41.920
surprises to learn

17
00:41.920 --> 00:45.890
that we're going to be expressing our intentions via the YAML format. And

18
00:45.890 --> 00:47.040
notice that it says a

19
00:47.040 --> 00:51.400
playbook can be comprised of one or more plays. So let me just draw a brief

20
00:51.400 --> 00:52.960
distinction right now.

21
00:52.960 --> 00:57.550
So let me just draw this out. Inside of a playbook, we can have multiple plays.

22
00:57.550 --> 00:58.480
And inside of these

23
00:58.480 --> 01:03.810
plays, we can have multiple tasks. So as per the diagram, play one has three
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:58.480 --> 01:03.810
plays, we can have multiple tasks. So as per the diagram, play one has three

24
01:03.810 --> 01:05.360
tasks, play two has

25
01:05.360 --> 01:09.830
only one task, and play three has two tasks. So what actually is the

26
01:09.830 --> 01:11.760
distinction between a play

27
01:11.760 --> 01:16.050
and a task? Well, the tasks themselves are actually the actions that we want to

28
01:16.050 --> 01:16.800
perform.

29
01:16.800 --> 01:21.280
So say for example, you wanted to restart a web server, that would be performed

30
01:21.280 --> 01:21.600
with an

31
01:21.600 --> 01:26.320
a task. However, when we're talking about a particular play, we are talking

32
01:26.320 --> 01:27.120
about applying

33
01:27.120 --> 01:32.100
actions to a particular set of hosts. So let's maybe say play one targets the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

33
01:27.120 --> 01:32.100
actions to a particular set of hosts. So let's maybe say play one targets the

34
01:32.100 --> 01:33.360
all group. And the

35
01:33.360 --> 01:37.610
three tasks that we perform say, for example, restarting the web server,

36
01:37.610 --> 01:39.040
creating a new directory,

37
01:39.040 --> 01:43.440
and maybe restarting a service. These three tasks are going to be applied at

38
01:43.440 --> 01:44.400
the same scope and

39
01:44.400 --> 01:49.430
apply to the all group. However, within the same playbook, the same file, if we

40
01:49.430 --> 01:50.320
want to perform

41
01:50.320 --> 01:55.080
another task that applies to a different set of hosts within our inventory, we

42
01:55.080 --> 01:56.160
create a new play.

43
01:56.160 --> 02:00.330
So the scope of play two, perhaps maybe it's only going to target the blue box,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

43
01:56.160 --> 02:00.330
So the scope of play two, perhaps maybe it's only going to target the blue box,

44
02:00.330 --> 02:00.720
and then we can

45
02:00.720 --> 02:05.020
specify the particular tasks that we want to execute. And then play three can

46
02:05.020 --> 02:06.160
just target the

47
02:06.160 --> 02:10.000
yellow box. And in this example, here, we're going to perform two tasks that

48
02:10.000 --> 02:10.880
just apply to

49
02:10.880 --> 02:15.020
the yellow box. So playbooks is where we can set our instructions. We can

50
02:15.020 --> 02:16.320
target different groups

51
02:16.320 --> 02:20.900
of devices for particular tasks using these plays. And what we actually want to

52
02:20.900 --> 02:22.000
do within the plays

53
02:22.000 --> 02:26.120
are defined within the tasks. So in this particular play, I'm going to use

54
02:26.120 --> 02:27.440
something known as an

55
02:27.440 --> 02:31.540
answerable module will definitely talk more about this in greater detail. But
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

55
02:27.440 --> 02:31.540
answerable module will definitely talk more about this in greater detail. But

56
02:31.540 --> 02:32.000
for now,

57
02:32.000 --> 02:36.380
just understand that these modules allow us to perform particular actions in a

58
02:36.380 --> 02:37.760
much easier way.

59
02:37.760 --> 02:42.080
The module I'm going to be looking at is the file module. As we can see here,

60
02:42.080 --> 02:42.720
it is going to allow

61
02:42.720 --> 02:47.080
us to manage files and file properties. So let's just click this in here. Now,

62
02:47.080 --> 02:47.840
one thing to note

63
02:47.840 --> 02:52.310
is that this happens to be a built in file module so that when you happen to

64
02:52.310 --> 02:54.240
install answerable core,

65
02:54.240 --> 02:58.230
the module is already going to be there. You don't have to do anything else. Be

66
02:58.230 --> 02:59.120
aware that when we

67
02:59.120 --> 03:03.200
use this module, you can reference the short name, which is just file. However,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:59.120 --> 03:03.200
use this module, you can reference the short name, which is just file. However,

68
03:03.200 --> 03:04.880
as it specifies here,

69
03:04.880 --> 03:08.520
they do recommend you use the fully qualified collection name, which in this

70
03:08.520 --> 03:09.360
case here would be

71
03:09.360 --> 03:14.140
answerable.builtin.file. So that is what we're going to do in this particular

72
03:14.140 --> 03:15.280
video. Now scrolling

73
03:15.280 --> 03:18.550
on down, you get to read the synopsis about what this particular module is

74
03:18.550 --> 03:19.760
going to do. So we can

75
03:19.760 --> 03:23.650
say attributes for files directory, sim links, whole bunch of stuff, as it says

76
03:23.650 --> 03:24.720
here. And very

77
03:24.720 --> 03:28.770
crucially, if we scroll on down, we can see these parameters. This effectively

78
03:28.770 --> 03:29.840
tells you how to

79
03:29.840 --> 03:34.300
actually use the module itself. So we can see an option here such as access
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

79
03:29.840 --> 03:34.300
actually use the module itself. So we can see an option here such as access

80
03:34.300 --> 03:35.680
time and below,

81
03:35.680 --> 03:39.630
it tells us that it takes a string value. Now a string value pretty much just

82
03:39.630 --> 03:40.800
means a regular word

83
03:40.800 --> 03:45.050
or sentence that type of thing, as opposed to a billion value, a true or false

84
03:45.050 --> 03:46.000
or an integer,

85
03:46.000 --> 03:49.680
which would be a number. So you can see here we have all of these options. If I

86
03:49.680 --> 03:51.280
go all the way down,

87
03:51.280 --> 03:55.500
say for example, to this parameter right here path, it tells us that this

88
03:55.500 --> 03:56.640
parameter happens to

89
03:56.640 --> 04:00.780
be required. Basically, when we use this particular module, we are also
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

89
03:56.640 --> 04:00.780
be required. Basically, when we use this particular module, we are also

90
04:00.780 --> 04:02.160
required to provide the

91
04:02.160 --> 04:06.500
specified file path. Same again, scrolling on down, we can see the state

92
04:06.500 --> 04:08.000
parameter. Once again,

93
04:08.000 --> 04:11.840
this takes a string value. And we can see some options here, we have the option

94
04:11.840 --> 04:12.640
absent. And it

95
04:12.640 --> 04:16.020
says here, if we specify absent directories are going to be recursive or

96
04:16.020 --> 04:17.600
deleted. Same with files

97
04:17.600 --> 04:22.440
and sim links will be unlinked. If we specify the state as directory, all the

98
04:22.440 --> 04:23.440
intermediate sub

99
04:23.440 --> 04:27.370
directories will be created. This would be like a nested directory structure.

100
04:27.370 --> 04:28.160
And we can see a whole

101
04:28.160 --> 04:32.660
bunch of other state options such as hard for a hard link, the word link for a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

101
04:28.160 --> 04:32.660
bunch of other state options such as hard for a hard link, the word link for a

102
04:32.660 --> 04:33.440
sim link. So

103
04:33.440 --> 04:36.920
really, as you can see here, we have a whole ton of options. The reason why I'm

104
04:36.920 --> 04:38.160
pointing this out,

105
04:38.160 --> 04:42.700
if you're like me, the first time you see this, you will probably think, hey,

106
04:42.700 --> 04:43.600
where are you getting

107
04:43.600 --> 04:48.350
all of these options from? How do you know which keywords to use? Pretty much.

108
04:48.350 --> 04:49.040
All you need to do

109
04:49.040 --> 04:53.480
is to read the answerable documentation, find out which parameters do which and

110
04:53.480 --> 04:54.320
which ones suit

111
04:54.320 --> 04:58.730
your needs. And you simply populate the playbook as follows. So let me write a

112
04:58.730 --> 05:00.080
playbook to do such a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

112
04:58.730 --> 05:00.080
playbook to do such a

113
05:00.080 --> 05:03.270
thing. So instead of my directory, I will just create a playbook. Let's just

114
05:03.270 --> 05:04.400
call this playbook

115
05:04.400 --> 05:08.230
one. And like I mentioned before, we can use the dot yaml extension. But the

116
05:08.230 --> 05:09.440
answerable convention

117
05:09.440 --> 05:13.410
is to use dot yml. So we'll stick with that. And let's just go in and edit this

118
05:13.410 --> 05:14.160
particular

119
05:14.160 --> 05:17.710
playbook. So let's go into insert mode. And the very first thing that I'm going

120
05:17.710 --> 05:18.640
to do is have my

121
05:18.640 --> 05:22.710
three hyphens to denote a yaml file. Okay, so to begin, what I'm going to do is

122
05:22.710 --> 05:23.600
to give my

123
05:23.600 --> 05:27.270
play a name. Now, one thing we're going to have is that within a playbook, we

124
05:27.270 --> 05:27.840
're going to have to

125
05:27.840 --> 05:32.780
specify our plays as an array object, even if we only happen to have one. So I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

125
05:27.840 --> 05:32.780
specify our plays as an array object, even if we only happen to have one. So I

126
05:32.780 --> 05:34.240
will use my hyphen.

127
05:34.240 --> 05:38.040
And then the key name, remember, we discussed in yaml, our hyphens allow us to

128
05:38.040 --> 05:38.960
specify an array.

129
05:38.960 --> 05:43.970
This is all I'm doing here. And I'll just say create nested directory. Now the

130
05:43.970 --> 05:44.320
next thing I'm

131
05:44.320 --> 05:48.610
going to do is to specify the hosts. This is pretty much the group of devices

132
05:48.610 --> 05:49.760
that I want to target

133
05:49.760 --> 05:53.870
from my inventory. Now recall, we actually have an all group within my

134
05:53.870 --> 05:55.680
inventory, which comprises

135
05:55.680 --> 05:59.740
of the blue box and yellow box. And it is that group that I want to target. So

136
05:59.740 --> 06:00.960
I'll just specify
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

136
05:59.740 --> 06:00.960
I'll just specify

137
06:00.960 --> 06:05.470
all now I'm going to create my tasks by specifying the task keywords. And once

138
06:05.470 --> 06:06.560
again, we can have

139
06:06.560 --> 06:10.960
multiple tasks. So I will use an array. So I'll have a hyphen and then specify

140
06:10.960 --> 06:11.840
the name key.

141
06:11.840 --> 06:15.630
And then say the hero will just say task one. And let me just wrap this in

142
06:15.630 --> 06:16.400
quotation marks.

143
06:16.400 --> 06:21.120
So it's all a string. And I'll just say creating three directories. Now at the

144
06:21.120 --> 06:22.560
same level of indentation,

145
06:22.560 --> 06:26.870
we want to specify that we're going to use the answerable built in file module

146
06:26.870 --> 06:27.440
recall,

147
06:27.440 --> 06:31.280
the FQDN we just saw in the documentation. That is what I'm referring to here.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

147
06:27.440 --> 06:31.280
the FQDN we just saw in the documentation. That is what I'm referring to here.

148
06:31.280 --> 06:32.560
If I hit enter,

149
06:32.560 --> 06:37.270
we go and dent it within, meaning that the next two options we supply are

150
06:37.270 --> 06:38.480
related to

151
06:38.480 --> 06:42.600
the built in file module. So remember, we happen to have the path parameter,

152
06:42.600 --> 06:43.360
which was required.

153
06:43.360 --> 06:46.400
So I would just type path. And now we'll just specify the path that I want to

154
06:46.400 --> 06:46.960
create. So within

155
06:46.960 --> 06:51.660
the temp directory, I will create their one, dirt to and their three. And again

156
06:51.660 --> 06:52.880
, at the same level

157
06:52.880 --> 06:57.370
of indentation, we want to specify the state which allows us to specify the

158
06:57.370 --> 06:58.640
directory. So what

159
06:58.640 --> 07:03.640
this now done, what I can do is press escape colon wq. Now with respect to our
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

159
06:58.640 --> 07:03.640
this now done, what I can do is press escape colon wq. Now with respect to our

160
07:03.640 --> 07:04.640
answerable ad

161
07:04.640 --> 07:08.170
hot commands, the way we could run these would just be to say answerable via

162
07:08.170 --> 07:09.040
the command line

163
07:09.040 --> 07:14.080
to actually run a playbook, we say answerable hyphen playbook, and then specify

164
07:14.080 --> 07:14.560
the name of the

165
07:14.560 --> 07:18.820
playbook that we've created, which in this case here is playbook one. So if I

166
07:18.820 --> 07:19.920
hit enter, the play

167
07:19.920 --> 07:24.930
runs, and we can see task one has executed, we have the summary suggesting we

168
07:24.930 --> 07:25.920
have a change,

169
07:25.920 --> 07:30.330
and we can see the blue box has changed as well as the yellow box indicating
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

169
07:25.920 --> 07:30.330
and we can see the blue box has changed as well as the yellow box indicating

170
07:30.330 --> 07:31.280
that the play

171
07:31.280 --> 07:35.590
targeted the correct scope, I eat it targeted the all group. And in the play

172
07:35.590 --> 07:36.560
recap, we see that we

173
07:36.560 --> 07:39.860
have an okay, nothing was unreachable, nothing was failed, and nothing was

174
07:39.860 --> 07:40.960
skipped and we have

175
07:40.960 --> 07:44.790
a change on both of these devices. So let's go and confirm this change actually

176
07:44.790 --> 07:45.600
took place.

177
07:45.600 --> 07:49.280
So let's go on to the yellow box here, we'll crack open a terminal, we'll see

178
07:49.280 --> 07:49.440
the

179
07:49.440 --> 07:53.440
enter the temp directory for doing LS, we can actually see here we have their

180
07:53.440 --> 07:54.480
one. So if I

181
07:54.480 --> 07:57.920
actually see the enter, their one and do an LS, we have a dirt to inside, we

182
07:57.920 --> 07:58.720
can see the enter

183
07:58.720 --> 08:04.280
to do an LS, we can see we also have a dirt three created. And similarly on the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

183
07:58.720 --> 08:04.280
to do an LS, we can see we also have a dirt three created. And similarly on the

184
08:04.280 --> 08:05.280
other device,

185
08:05.280 --> 08:09.140
the blue box, once again, we see the enter the temp directory to an LS, we can

186
08:09.140 --> 08:09.840
see we have

187
08:09.840 --> 08:14.640
their one right here, see the enter their one LS, there is their two, going to

188
08:14.640 --> 08:15.440
that LS,

189
08:15.440 --> 08:19.840
and we also have their three right here. Now similarly, if you wanted to have a

190
08:19.840 --> 08:21.040
second task,

191
08:21.040 --> 08:24.640
and let's do it so that we actually create a fourth directory here, so I'll say

192
08:24.640 --> 08:25.520
therefore.

193
08:25.520 --> 08:29.520
Now we also want to add a second task, again, this is contained within the same

194
08:29.520 --> 08:30.000
place, so we're
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

195
08:30.000 --> 08:34.440
going to still target both devices, I either all group. So again, I will just

196
08:34.440 --> 08:35.840
have my hyphen

197
08:35.840 --> 08:41.070
specify the name, and this time we can have the second task to, or just say add

198
08:41.070 --> 08:42.880
content to fail.

199
08:42.880 --> 08:46.060
Now in this case here, I'm actually going to use the Ansible built in shell

200
08:46.060 --> 08:46.480
module,

201
08:46.480 --> 08:49.460
just like we saw with the add hot command, this is going to allow us to execute

202
08:49.460 --> 08:50.560
shell commands,

203
08:50.560 --> 08:55.440
and all I'm going to do here is say echo, this is a test, and I'm going to

204
08:55.440 --> 08:56.480
redirect that

205
08:56.480 --> 09:00.570
into the temp directory, and let's maybe put this inside of dirt too, what we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

205
08:56.480 --> 09:00.570
into the temp directory, and let's maybe put this inside of dirt too, what we

206
09:00.570 --> 09:01.280
'll do is create a

207
09:01.280 --> 09:06.800
file called nuggets.txt. So this will be our second task, so let's try this

208
09:06.800 --> 09:07.520
right and quit,

209
09:07.520 --> 09:11.630
clear the screen, let's rerun the playbook, hit enter, and the playbook

210
09:11.630 --> 09:13.200
executes, we get a summary,

211
09:13.200 --> 09:16.880
here we have task one, it says creating three directories, I really should have

212
09:16.880 --> 09:17.360
updated the

213
09:17.360 --> 09:20.320
name to say creating four directories, because that is what it's actually done,

214
09:20.320 --> 09:21.040
but nevertheless,

215
09:21.040 --> 09:25.600
let's not worry about that, and the second task is adding content to a file,

216
09:25.600 --> 09:26.320
and that is also

217
09:26.320 --> 09:30.250
changed, so let's go and test out the results then, so back on the blue box, we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

217
09:26.320 --> 09:30.250
changed, so let's go and test out the results then, so back on the blue box, we

218
09:30.250 --> 09:30.720
can go into the

219
09:30.720 --> 09:34.740
temp directory, do an LS, here we have our dirt one, if I do an LS, we have

220
09:34.740 --> 09:36.000
dirt two, now inside of

221
09:36.000 --> 09:40.750
dirt two, if we do an LS, notice we also have the nuggets.txt file, which we

222
09:40.750 --> 09:42.560
can actually cat right here,

223
09:42.560 --> 09:46.750
and it says this is a test, and inside of here also, we have dirt three, and

224
09:46.750 --> 09:48.080
inside of that,

225
09:48.080 --> 09:51.540
we should also have, which we can see here, the fourth directory we just

226
09:51.540 --> 09:52.880
created, and similarly,

227
09:52.880 --> 09:57.510
if we go on to the yellow box, go into the temp directory, do an LS, there is

228
09:57.510 --> 09:58.400
our dirt one,

229
09:58.400 --> 10:02.580
let's go into dirt two, inside of dirt two, we also have the newly created file
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

229
09:58.400 --> 10:02.580
let's go into dirt two, inside of dirt two, we also have the newly created file

230
10:02.580 --> 10:03.360
with the content,

231
10:03.360 --> 10:07.660
this is a test, and also we have inside of dirt three, the newly created dirt

232
10:07.660 --> 10:08.800
four directory,

233
10:08.800 --> 10:13.120
so those are the very very basics of creating a simple playbook, as we can see

234
10:13.120 --> 10:14.000
here, we created

235
10:14.000 --> 10:18.950
one play with multiple tasks, which allowed us to perform two different actions

236
10:18.950 --> 10:20.080
against the same

237
10:20.080 --> 10:22.080
set of hosts.
