WEBVTT

1
00:00.000 --> 00:04.400
So friends, now that we understand how to deal with facts as well as how to set

2
00:04.400 --> 00:05.680
variables utilizing

3
00:05.680 --> 00:09.180
the register keyword, now what I want to talk to you about is how we can

4
00:09.180 --> 00:10.560
utilize handlers within

5
00:10.560 --> 00:14.580
our playbook. So with that said, let's just get to it. So let's go to the

6
00:14.580 --> 00:15.760
answerable documentation

7
00:15.760 --> 00:19.780
and find out what these handlers actually do for us. So quite simply just

8
00:19.780 --> 00:20.320
search for

9
00:20.320 --> 00:24.650
answerable handlers if we click this top link right here scrolling on down we

10
00:24.650 --> 00:25.440
can see here

11
00:25.440 --> 00:29.990
immediately handlers allow us to run operations on a change. So really what we

12
00:29.990 --> 00:30.960
're thinking about
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

12
00:29.990 --> 00:30.960
're thinking about

13
00:30.960 --> 00:35.830
here is conditional logic once again. So this time imagine that we only want to

14
00:35.830 --> 00:37.120
run a particular

15
00:37.120 --> 00:42.400
task depending on the result of the previous task. The example they gave here

16
00:42.400 --> 00:43.600
is let's imagine you

17
00:43.600 --> 00:47.840
make some type of configuration change to a particular service and for that

18
00:47.840 --> 00:49.040
change to actually take

19
00:49.040 --> 00:52.670
an effect. What we would have to do would be to restart the service now we don

20
00:52.670 --> 00:53.440
't just want to

21
00:53.440 --> 00:57.670
restart the service if the configuration changes did not actually make any

22
00:57.670 --> 00:58.880
difference. So we don't

23
00:58.880 --> 01:04.000
just want to automatically restart the service. We want to push a configuration
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:58.880 --> 01:04.000
just want to automatically restart the service. We want to push a configuration

24
01:04.000 --> 01:04.320
and if the

25
01:04.320 --> 01:09.550
configuration happens to make a change to the system then and only then would

26
01:09.550 --> 01:10.320
we like to have

27
01:10.320 --> 01:16.080
a task to restart that service. Now notice here it says handlers are tasks that

28
01:16.080 --> 01:17.680
are only run when

29
01:17.680 --> 01:22.510
notified this concept here really is important notification. This is how we

30
01:22.510 --> 01:23.600
actually inform

31
01:23.600 --> 01:28.420
the handler to run. So let me go and give you a simple example then okay. Now

32
01:28.420 --> 01:28.960
the first thing I

33
01:28.960 --> 01:32.970
want to do is I want to ensure that I have engine X running on both of my
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

33
01:28.960 --> 01:32.970
want to do is I want to ensure that I have engine X running on both of my

34
01:32.970 --> 01:34.400
managed devices. Now you'll

35
01:34.400 --> 01:39.020
notice if I say for example log into the yellow box and I just do a system CTL

36
01:39.020 --> 01:40.800
status on engine X

37
01:40.800 --> 01:45.150
notice I actually don't have engine X on this device the same is true of my

38
01:45.150 --> 01:46.080
blue box. So what

39
01:46.080 --> 01:51.420
I will do quickly is I will just run an answerable playbook to install engine X

40
01:51.420 --> 01:52.880
on both of my devices

41
01:52.880 --> 01:57.010
right now. So let's create play six dot y amount and we're going to insert mode

42
01:57.010 --> 01:57.760
have my three

43
01:57.760 --> 02:01.180
high friends get the playbook a name which is called this playbook six because
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

43
01:57.760 --> 02:01.180
high friends get the playbook a name which is called this playbook six because

44
02:01.180 --> 02:02.400
I have no creativity

45
02:02.400 --> 02:06.820
we're going to target the all group I will just say gather facts I specify my

46
02:06.820 --> 02:07.840
tasks and the task

47
02:07.840 --> 02:11.920
here it's just going to be called install engine X and because I'm on red hat I

48
02:11.920 --> 02:13.120
'm going to use the

49
02:13.120 --> 02:18.250
built in DNF module and the package I want to install is engine X and I want

50
02:18.250 --> 02:19.360
the state to be

51
02:19.360 --> 02:24.130
present and just before I do anything what I will do is I'll just say gather

52
02:24.130 --> 02:25.440
facts is the value

53
02:25.440 --> 02:30.320
false so let's save this and run the playbook. So as we can see here the change
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

53
02:25.440 --> 02:30.320
false so let's save this and run the playbook. So as we can see here the change

54
02:30.320 --> 02:31.200
has been made we

55
02:31.200 --> 02:35.500
should now have engine X on both of our devices. Now here is the thing if I

56
02:35.500 --> 02:36.800
happen to go to my

57
02:36.800 --> 02:41.990
devices right here if I do system cta status on engine X we can see here I now

58
02:41.990 --> 02:43.360
have engine X but

59
02:43.360 --> 02:47.120
as it stands right now it is inactive so what I'm going to do here actually and

60
02:47.120 --> 02:47.840
I probably should

61
02:47.840 --> 02:51.210
have done this within the playbook but nevertheless let's just do this manually

62
02:51.210 --> 02:51.840
start the server is

63
02:51.840 --> 02:56.000
typing my password now it should be running which indeed it is and let's just

64
02:56.000 --> 02:56.880
do the same thing

65
02:56.880 --> 03:02.520
on the blue box or do pseudo system cta start engine X. If I browse to local
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

65
02:56.880 --> 03:02.520
on the blue box or do pseudo system cta start engine X. If I browse to local

66
03:02.520 --> 03:03.360
host we can see

67
03:03.360 --> 03:07.600
here we have the content of my engine X webserver. So here's what I want to do

68
03:07.600 --> 03:08.320
I want to actually

69
03:08.320 --> 03:13.930
make a change to the actual HTML file that has been pulled in by engine X and

70
03:13.930 --> 03:14.800
when I make that

71
03:14.800 --> 03:19.420
configuration change for that to actually operate what I'm going to do is I

72
03:19.420 --> 03:20.640
want to restart my

73
03:20.640 --> 03:25.120
server now I'm going to restart it only if a change happens to be detected that

74
03:25.120 --> 03:25.680
means we're

75
03:25.680 --> 03:30.500
going to use a handler. So let me create a new playbook called play seven dot y
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

75
03:25.680 --> 03:30.500
going to use a handler. So let me create a new playbook called play seven dot y

76
03:30.500 --> 03:31.200
ml let's just do

77
03:31.200 --> 03:34.790
the usual have a three hyphens gather playbook a name which is called this

78
03:34.790 --> 03:35.920
playbook seven testing

79
03:35.920 --> 03:40.580
handlers what we're targeting we're targeting all our set gather facts to false

80
03:40.580 --> 03:41.680
and now define my

81
03:41.680 --> 03:46.590
tasks and I'll just say copy HTML file to device so what I'll do is I'll use

82
03:46.590 --> 03:47.840
the answer will build

83
03:47.840 --> 03:52.350
10 template module and for a source I'm going to specify a file within my

84
03:52.350 --> 03:53.920
templates directory and

85
03:53.920 --> 03:59.260
it's just going to be called index dot j2 okay and the destination this time it

86
03:59.260 --> 04:00.240
's going to be user
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

86
03:59.260 --> 04:00.240
's going to be user

87
04:00.240 --> 04:06.080
share engine X html and then the file will be index dot html this is a common

88
04:06.080 --> 04:06.640
and well-known

89
04:06.640 --> 04:11.720
path the engine X uses to consume its index file now the interesting part here

90
04:11.720 --> 04:12.400
is this part we're

91
04:12.400 --> 04:17.520
going to say notify this is how we notify our handler so pretty much I can give

92
04:17.520 --> 04:18.560
any name I so

93
04:18.560 --> 04:22.360
wish here we just want to make sure it syncs up with whatever we call the

94
04:22.360 --> 04:23.680
handler so I could say

95
04:23.680 --> 04:29.540
restart engine X upon change okay and now I'm going to do it at the same level

96
04:29.540 --> 04:31.360
of indentation as my
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

96
04:29.540 --> 04:31.360
of indentation as my

97
04:31.360 --> 04:35.620
tasks I want to actually define my handlers now similar I'm going to give a

98
04:35.620 --> 04:36.640
name now this is the

99
04:36.640 --> 04:41.500
interesting part this here is what we're going to sync up with okay so I named

100
04:41.500 --> 04:42.960
the actual notification

101
04:42.960 --> 04:47.400
restart engine X upon change that is going to be the same one I use right here

102
04:47.400 --> 04:48.480
so restart engine

103
04:48.480 --> 04:52.990
X upon change and now I specify what it is I want to do so I'm going to say my

104
04:52.990 --> 04:55.040
service is engine X

105
04:55.040 --> 05:00.370
and I'm going to specify that the state should be restarted so basically when I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

105
04:55.040 --> 05:00.370
and I'm going to specify that the state should be restarted so basically when I

106
05:00.370 --> 05:01.440
copy over this

107
05:01.440 --> 05:05.870
particular file to this particular destination on both of my devices if I'm

108
05:05.870 --> 05:06.960
able to text that this

109
05:06.960 --> 05:11.610
particular file has changed we want to notify this particular handler and this

110
05:11.610 --> 05:12.960
particular handler

111
05:12.960 --> 05:16.620
is this one right here and what is the action we're going to perform we're

112
05:16.620 --> 05:17.360
going to target the

113
05:17.360 --> 05:22.670
engine X service and we're going to restart it again it will only happen if a

114
05:22.670 --> 05:24.160
change is detected

115
05:24.160 --> 05:27.600
right here so what I'm going to have to do is I'm going to have to create this

116
05:27.600 --> 05:29.200
templates index.j2

117
05:29.200 --> 05:33.400
file right here inside of my templates directory I'm going to have a file
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

117
05:29.200 --> 05:33.400
file right here inside of my templates directory I'm going to have a file

118
05:33.400 --> 05:35.280
called index.j2 and this

119
05:35.280 --> 05:38.770
here is the simple template I'm going to use basically I'm going to make the

120
05:38.770 --> 05:39.760
change welcome to

121
05:39.760 --> 05:43.770
John's engine X server now what I will do here within the actual scale I'll

122
05:43.770 --> 05:44.960
actually give you a

123
05:44.960 --> 05:49.470
copy of this particular template so you can just modify as needed okay so let

124
05:49.470 --> 05:50.080
me just save this

125
05:50.080 --> 05:53.560
right here now one thing I want to show you right now see if I go back to my

126
05:53.560 --> 05:54.720
engine X server on

127
05:54.720 --> 05:58.730
say for example this device we just full screen this temporarily if I just

128
05:58.730 --> 05:59.760
arrow up and check the

129
05:59.760 --> 06:04.990
status notice this server has been running for six minutes okay now when I run
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

129
05:59.760 --> 06:04.990
status notice this server has been running for six minutes okay now when I run

130
06:04.990 --> 06:06.080
this playbook

131
06:06.080 --> 06:10.480
this is going to restart and the clock will restart as well so I'll say answer

132
06:10.480 --> 06:11.120
my playbook

133
06:11.120 --> 06:15.840
place 7.yml and to answer so now be copy over the changes and it will detect

134
06:15.840 --> 06:16.800
that we have a change

135
06:16.800 --> 06:21.020
and therefore the handler takes effect and as we can see the handler has indeed

136
06:21.020 --> 06:21.600
made a change we

137
06:21.600 --> 06:25.940
have restarted the engine X server so if I go back to my yellow box and I just

138
06:25.940 --> 06:26.880
arrow up and run the

139
06:26.880 --> 06:30.950
command once again look at this right here the server has only been running for
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

139
06:26.880 --> 06:30.950
command once again look at this right here the server has only been running for

140
06:30.950 --> 06:32.320
23 seconds it was

141
06:32.320 --> 06:37.350
indeed restarted now the cool thing is if I actually go to my browser here

142
06:37.350 --> 06:38.160
check this out right here

143
06:38.160 --> 06:42.720
the engine X server now has these new changes welcome to john's engine X server

144
06:42.720 --> 06:43.440
now the cool

145
06:43.440 --> 06:48.230
thing is if I go back to my playbook and I rerun it and copy the same

146
06:48.230 --> 06:50.560
configuration file over again

147
06:50.560 --> 06:55.330
look at the actual output because there was no change the same file is already

148
06:55.330 --> 06:56.480
on the system

149
06:56.480 --> 07:00.940
because of that the handler didn't actually execute therefore we didn't
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

149
06:56.480 --> 07:00.940
because of that the handler didn't actually execute therefore we didn't

150
07:00.940 --> 07:02.480
actually restart the server

151
07:02.480 --> 07:06.750
unnecessarily so once again if I go back to my terminal on this particular

152
07:06.750 --> 07:07.760
device arrow up

153
07:07.760 --> 07:12.050
we can see here the server is still running it wasn't actually restarted the

154
07:12.050 --> 07:12.800
clock didn't go

155
07:12.800 --> 07:16.600
back to zero again now one thing I just want to point out with respect to the

156
07:16.600 --> 07:17.440
notification

157
07:17.440 --> 07:22.560
notice the way we had to sync up this particular name with this particular name

158
07:22.560 --> 07:24.160
here now the reality

159
07:24.160 --> 07:28.710
is we don't actually have to do that we can also utilize the lesson directive

160
07:28.710 --> 07:29.600
so what that will

161
07:29.600 --> 07:33.950
allow me to do it will allow me to mismatch the name of the handler and still
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

161
07:29.600 --> 07:33.950
allow me to do it will allow me to mismatch the name of the handler and still

162
07:33.950 --> 07:34.960
also execute let

163
07:34.960 --> 07:38.290
me show you what I mean so right now if I just happened to mismatch this say

164
07:38.290 --> 07:39.200
for example and

165
07:39.200 --> 07:42.950
let me go into the templates I'm going to index.j2 and we'll make an actual

166
07:42.950 --> 07:44.240
change here let's just say

167
07:44.240 --> 07:48.510
blast or a change is actually made save here the change will be made but the

168
07:48.510 --> 07:49.600
handler is not going

169
07:49.600 --> 07:54.480
to be called because the mismatch okay so run this notice this we have an error

170
07:54.480 --> 07:55.840
the handler was not

171
07:55.840 --> 08:00.530
found okay so check this out in here now as opposed to having to match these up
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

171
07:55.840 --> 08:00.530
found okay so check this out in here now as opposed to having to match these up

172
08:00.530 --> 08:01.520
what I will do here

173
08:01.520 --> 08:05.800
instead let's just give this a shorter name like let's just say engine x

174
08:05.800 --> 08:07.120
handler okay now what I

175
08:07.120 --> 08:11.070
will do here is I will use the lesson directive and I will match that up with

176
08:11.070 --> 08:12.480
the notified directive

177
08:12.480 --> 08:18.080
so notice this matches exactly this but what it does it frees us up to give the

178
08:18.080 --> 08:19.360
task a rather long

179
08:19.360 --> 08:25.220
and descriptive name so this task could be called restart engine x server when

180
08:25.220 --> 08:26.960
a config has changed

181
08:26.960 --> 08:31.080
so as opposed to having to copy this long descriptive sentence right here
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

181
08:26.960 --> 08:31.080
so as opposed to having to copy this long descriptive sentence right here

182
08:31.080 --> 08:32.080
instead we can just use

183
08:32.080 --> 08:35.840
shorthand by making use of the lesson directive so we'll go back into the

184
08:35.840 --> 08:37.040
templates once again

185
08:37.040 --> 08:40.820
change this back maybe add in some exclamation marks so that there is a new

186
08:40.820 --> 08:41.680
change right at the

187
08:41.680 --> 08:45.150
changes go back and now we'll run the playbook and because you have a change we

188
08:45.150 --> 08:46.240
're now going to call

189
08:46.240 --> 08:50.450
the handler once again and indeed the change has taken effect so if we go back

190
08:50.450 --> 08:51.280
to our devices

191
08:51.280 --> 08:55.230
let's go to our server and we'll refresh and check this out we now have the

192
08:55.230 --> 08:56.480
changes with the three ex

193
08:56.480 --> 08:59.100
Commission marks.
