WEBVTT

1
00:00.000 --> 00:02.600
So friends, now that we know how to create archives,

2
00:02.600 --> 00:05.120
as well as how we can unarchive our files,

3
00:05.120 --> 00:08.000
let's add some more attention to how we can handle errors

4
00:08.000 --> 00:09.580
in our Ansible Playbooks.

5
00:09.580 --> 00:11.280
So you may notice that when you happen

6
00:11.280 --> 00:13.640
to create a particular task in Ansible

7
00:13.640 --> 00:15.640
and you encounter an error,

8
00:15.640 --> 00:19.120
what effectively happens is the playbook just crashes.

9
00:19.120 --> 00:20.840
So what this actually means is that

10
00:20.840 --> 00:22.600
if you happen to have a whole bunch of tasks,

11
00:22.600 --> 00:25.400
let's maybe say task one and task two

12
00:25.400 --> 00:27.360
and perhaps even task three,

13
00:27.360 --> 00:29.200
let's suppose we happen to have an error

14
00:29.200 --> 00:33.000
in task one, maybe you try to copy a file to a destination
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

14
00:29.200 --> 00:33.000
in task one, maybe you try to copy a file to a destination

15
00:33.000 --> 00:34.840
and that file doesn't even exist.

16
00:34.840 --> 00:37.560
So the module fails and effectively the playbook crashes.

17
00:37.560 --> 00:41.040
What this effectively means is that these two tasks,

18
00:41.040 --> 00:43.840
even though they can be correctly configured,

19
00:43.840 --> 00:45.320
well, they're not even going to get the chance

20
00:45.320 --> 00:47.640
to be configured because basically Ansible

21
00:47.640 --> 00:49.760
is going to work any sequential manner.

22
00:49.760 --> 00:51.680
And as soon as we encounter this error here,

23
00:51.680 --> 00:53.320
the playbook is just going to stop.

24
00:53.320 --> 00:55.080
So what we want to be able to do is to learn

25
00:55.080 --> 00:56.840
how we can handle our errors.

26
00:56.840 --> 00:58.880
So that if we happen to encounter an error,

27
00:58.880 --> 01:01.080
let's maybe say again on task one,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

27
00:58.880 --> 01:01.080
let's maybe say again on task one,

28
01:01.080 --> 01:03.640
the playbook acknowledges that error

29
01:03.640 --> 01:05.440
and it handles it appropriately

30
01:05.440 --> 01:08.560
and then continues on, meaning that task two and task three,

31
01:08.560 --> 01:10.160
if they are correctly configured,

32
01:10.160 --> 01:12.080
they can still actually execute.

33
01:12.080 --> 01:13.480
Now, just as a quick note,

34
01:13.480 --> 01:14.880
if you happen to have experience

35
01:14.880 --> 01:16.600
with the Python programming language,

36
01:16.600 --> 01:18.560
not that you have to note to use Ansible,

37
01:18.560 --> 01:20.200
but many people who are using Ansible

38
01:20.200 --> 01:21.760
do have familiarity.

39
01:21.760 --> 01:23.720
What we're going to talk about here is quite similar

40
01:23.720 --> 01:26.560
to what is known as a try and accept block.

41
01:26.560 --> 01:28.840
Now, for those of you that do not have any experience,

42
01:28.840 --> 01:30.680
with Python, let me talk to you about
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

42
01:28.840 --> 01:30.680
with Python, let me talk to you about

43
01:30.680 --> 01:32.280
what we're actually talking about here.

44
01:32.280 --> 01:33.600
In the case of Python,

45
01:33.600 --> 01:35.440
when you want to try a piece of code

46
01:35.440 --> 01:36.800
and hope that it works,

47
01:36.800 --> 01:38.840
you can effectively use the try keyword

48
01:38.840 --> 01:41.040
and any code within this statement

49
01:41.040 --> 01:43.160
is going to be attempted to be ran.

50
01:43.160 --> 01:46.240
However, if you happen to encounter an error,

51
01:46.240 --> 01:48.760
you can create what is known as an accept block

52
01:48.760 --> 01:50.760
and then specify what you want to happen

53
01:50.760 --> 01:52.840
if the try block happens to fail.

54
01:52.840 --> 01:54.120
So whilst in Python,

55
01:54.120 --> 01:57.080
you would use the try keyword in Ansible,

56
01:57.080 --> 01:59.920
we actually specify the block keyword

57
01:59.920 --> 02:01.960
and with respect to the accept block
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
01:59.920 --> 02:01.960
and with respect to the accept block

58
02:01.960 --> 02:03.760
that you would specify in Python,

59
02:03.760 --> 02:07.400
what you actually say in Ansible as the keyword rescue.

60
02:07.400 --> 02:09.560
So those of you with Python experience

61
02:09.560 --> 02:12.040
probably now understand these concepts already,

62
02:12.040 --> 02:13.200
but for those of you that don't,

63
02:13.200 --> 02:16.360
let's dive into an example and see how it actually operates.

64
02:16.360 --> 02:19.840
So let's go on and create a playbook called play3.yml.

65
02:19.840 --> 02:22.520
So we're going to insert more TAVR3 hyphens at the top,

66
02:22.520 --> 02:23.400
give it a name,

67
02:23.400 --> 02:26.200
and we just say play3 handling errors

68
02:26.200 --> 02:27.800
and try to spell that correctly, John.

69
02:27.800 --> 02:28.720
There we go.

70
02:28.720 --> 02:30.400
So let's now target the host.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

70
02:28.720 --> 02:30.400
So let's now target the host.

71
02:30.400 --> 02:32.000
Let's just go with the blue box once again.

72
02:32.000 --> 02:34.080
We'll set gather facts to be false.

73
02:34.080 --> 02:35.720
And now let's define our tasks.

74
02:35.720 --> 02:37.160
So the first task here,

75
02:37.160 --> 02:39.960
let's imagine we want to copy a particular file here.

76
02:39.960 --> 02:42.920
So we'll use the Ansible built-in copy module

77
02:42.920 --> 02:46.040
and I will specify as the source is going to be,

78
02:46.040 --> 02:48.320
so let's go into my home Ansible directory

79
02:48.320 --> 02:51.840
and let's just specify a file called random.txt.

80
02:51.840 --> 02:53.320
And then specify we want to copy it

81
02:53.320 --> 02:55.280
into the nugget user directory

82
02:55.280 --> 02:57.680
and the file can also be called random.txt.

83
02:57.680 --> 02:59.480
Now the problem is going to be here

84
02:59.480 --> 03:01.680
that on my control mode,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

84
02:59.480 --> 03:01.680
that on my control mode,

85
03:01.680 --> 03:03.360
this file here does not actually exist.

86
03:03.360 --> 03:04.240
Let me show you that, okay?

87
03:04.240 --> 03:06.440
So we can see the directory I'm into an LS.

88
03:06.440 --> 03:09.520
Notice we actually don't see this random.txt file.

89
03:09.520 --> 03:11.120
So we're going to encounter an error.

90
03:11.120 --> 03:13.800
So let's continue on and have more tasks.

91
03:13.800 --> 03:15.400
Let's just call this do something else.

92
03:15.400 --> 03:17.000
Now this can be absolutely anything.

93
03:17.000 --> 03:18.760
This could be perhaps copying more files.

94
03:18.760 --> 03:20.160
It could be creating new directories.

95
03:20.160 --> 03:21.920
It could be compressing files, all that stuff.

96
03:21.920 --> 03:23.280
It doesn't actually matter.

97
03:23.280 --> 03:25.280
In the case here, just for the example,

98
03:25.280 --> 03:27.080
let me just print out a simple message, okay?

99
03:27.080 --> 03:29.520
So I'll use the Ansible built-in debug module

100
03:29.520 --> 03:31.000
and the message is going to be,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

100
03:29.520 --> 03:31.000
and the message is going to be,

101
03:31.000 --> 03:33.360
this is the second task, okay?

102
03:33.360 --> 03:34.480
Super, super simple.

103
03:34.480 --> 03:36.400
Now as it transpires right now, like I say,

104
03:36.400 --> 03:38.160
because we're going to have a failure

105
03:38.160 --> 03:40.240
and this particular task here,

106
03:40.240 --> 03:41.680
the playbook is going to crash

107
03:41.680 --> 03:45.720
and therefore this task here will not actually execute.

108
03:45.720 --> 03:47.480
So let's just save this

109
03:47.480 --> 03:48.840
and I'll just say Ansible,

110
03:48.840 --> 03:51.760
play book, play three dot YML and to enter

111
03:51.760 --> 03:53.480
and look at this right here.

112
03:53.480 --> 03:56.600
We just get an exception, pretty much an error,

113
03:56.600 --> 04:00.080
specifying that we could not actually find or access
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

113
03:56.600 --> 04:00.080
specifying that we could not actually find or access

114
04:00.080 --> 04:03.040
this particular text file on the Ansible controller.

115
04:03.040 --> 04:04.160
So this is the error.

116
04:04.160 --> 04:07.600
Notice though, crucially, where is the second task?

117
04:07.600 --> 04:08.920
It didn't actually execute.

118
04:08.920 --> 04:10.920
We don't get to see that printed message.

119
04:10.920 --> 04:12.560
So here's what we can do.

120
04:12.560 --> 04:14.640
I think we'll go back into the actual playbook here.

121
04:14.640 --> 04:16.600
So let's go in and say play three dot YML.

122
04:16.600 --> 04:19.560
What I'm going to do here is I'm going to create a block

123
04:19.560 --> 04:21.400
and I'm going to effectively indent everything

124
04:21.400 --> 04:23.080
so let me just give this a new name.

125
04:23.080 --> 04:25.440
So let's say attempt to copy the file

126
04:25.440 --> 04:27.400
and let me just fix my indentation.

127
04:27.400 --> 04:29.880
So let's move this in for spaces and again,

128
04:29.880 --> 04:30.880
for spaces again.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

128
04:29.880 --> 04:30.880
for spaces again.

129
04:30.880 --> 04:33.440
And let's maybe just change this name to do multiple tasks

130
04:33.440 --> 04:34.840
to be a little bit more descriptive.

131
04:34.840 --> 04:37.480
Now at the same level of indentation as our block,

132
04:37.480 --> 04:39.320
we want to use the rescue keyboard.

133
04:39.320 --> 04:42.120
So I'll say rescue and now I'll give this a name

134
04:42.120 --> 04:43.800
and I'll just say handle the error.

135
04:43.800 --> 04:46.400
Now again, the way you handle this is entirely up to you.

136
04:46.400 --> 04:49.120
You can perhaps maybe create a log file of the error.

137
04:49.120 --> 04:49.960
In the case here,

138
04:49.960 --> 04:52.160
what is used the answer will build in debug module

139
04:52.160 --> 04:53.560
to simply print out a message.

140
04:53.560 --> 04:54.880
What is that message going to be?

141
04:54.880 --> 04:56.840
Just say the copy task failed.

142
04:56.840 --> 04:58.600
Now with respect to the next task,

143
04:58.600 --> 05:01.840
effectively our simulation task that is not really doing much.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
04:58.600 --> 05:01.840
effectively our simulation task that is not really doing much.

144
05:01.840 --> 05:04.560
I want this to always execute regardless, okay?

145
05:04.560 --> 05:06.400
So at the same level of indentation

146
05:06.400 --> 05:08.320
as the block and rescue keywords,

147
05:08.320 --> 05:09.760
let me have the always keyword

148
05:09.760 --> 05:12.080
and now just indent everything within once again.

149
05:12.080 --> 05:13.880
So again, for spaces, indent it.

150
05:13.880 --> 05:14.720
Perfect.

151
05:14.720 --> 05:15.560
So now we have this new playbook.

152
05:15.560 --> 05:17.480
So whilst this file here does not exist,

153
05:17.480 --> 05:19.120
we're going to encounter an error,

154
05:19.120 --> 05:20.440
as opposed to crashing,

155
05:20.440 --> 05:24.120
we'll specify that we want to print this particular message

156
05:24.120 --> 05:26.760
and then we will continue on with the other tasks

157
05:26.760 --> 05:27.760
that you want to execute.

158
05:27.760 --> 05:29.160
So let's go for this here.

159
05:29.160 --> 05:31.200
I'll say answer will playbook, play through.yml
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

159
05:29.160 --> 05:31.200
I'll say answer will playbook, play through.yml

160
05:31.200 --> 05:32.960
and to answer, look at this right here.

161
05:32.960 --> 05:35.940
We actually get an error message as we saw before,

162
05:35.940 --> 05:37.360
but this time we continue on,

163
05:37.360 --> 05:40.440
we specify that hey, the copy task happened to fail

164
05:40.440 --> 05:43.720
and now the next task here is going to execute.

165
05:43.720 --> 05:46.200
Now again, the example is a little bit frivolous here,

166
05:46.200 --> 05:48.400
but you can imagine this could be any particular task

167
05:48.400 --> 05:49.820
no matter how advanced.
