WEBVTT

1
00:00.000 --> 00:03.720
So throughout this skill, what we've been effectively doing is testing

2
00:03.720 --> 00:05.060
different parts of our system,

3
00:05.060 --> 00:10.080
testing the memory, testing the CPU, that type of thing, as well as learning

4
00:10.080 --> 00:11.740
how to manipulate particular values.

5
00:11.740 --> 00:17.220
Now, what we can do also is to utilize a very particular Ansible module,

6
00:17.220 --> 00:22.100
which is really designed for testing purposes, and that is the assert module.

7
00:22.100 --> 00:25.540
So how about we go to the documentation and have a look at what this module

8
00:25.540 --> 00:26.500
actually does.

9
00:26.620 --> 00:30.600
So if you just happen to search for Ansible Assert and collect this top link
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

9
00:26.620 --> 00:30.600
So if you just happen to search for Ansible Assert and collect this top link

10
00:30.600 --> 00:31.300
right here,

11
00:31.300 --> 00:34.620
as you can see straight away, this module is part of Ansible Core,

12
00:34.620 --> 00:37.840
so you don't actually have to install it via Ansible Galaxy.

13
00:37.840 --> 00:41.680
And if you want to use it, you can specify the fully qualified collection name

14
00:41.680 --> 00:43.840
of Ansible.builtin.assert.

15
00:43.840 --> 00:47.320
Now if we just scroll on down a little bit here, we can see the synopsis.

16
00:47.320 --> 00:52.120
It says here that its whole purpose is to assert that given expressions are

17
00:52.120 --> 00:54.820
true with an optional custom message.

18
00:54.820 --> 00:58.380
So really, this is what we've been doing with respect to our test.

19
00:58.380 --> 01:01.600
We're testing it as something true or as something false, as this particular
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

19
00:58.380 --> 01:01.600
We're testing it as something true or as something false, as this particular

20
01:01.600 --> 01:03.340
number over this particular value,

21
01:03.340 --> 01:06.740
is that true or as that false, and then have a message.

22
01:06.740 --> 01:11.110
And this particular module we have this nice elegant way to specify within the

23
01:11.110 --> 01:11.680
same task,

24
01:11.680 --> 01:15.180
if a test happens to fail or if a test happens to pass.

25
01:15.180 --> 01:19.810
So if we scroll on down here, we can see we happen to have a parameter fail_msg

26
01:19.810 --> 01:20.100
.

27
01:20.100 --> 01:25.010
This is how we specify the custom message that we want to use when a failure

28
01:25.010 --> 01:25.900
happens.

29
01:25.900 --> 01:29.780
Conversely, if we scroll on down, we also have this other option,

30
01:29.780 --> 01:35.430
success_msg. This is the custom message that you specify when you have a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

30
01:29.780 --> 01:35.430
success_msg. This is the custom message that you specify when you have a

31
01:35.430 --> 01:38.220
successful assertion i.e. when the test pass.

32
01:38.220 --> 01:41.980
Then if we scroll on down, we also have the that keyword.

33
01:41.980 --> 01:44.700
And as we can see here, this one is actually required.

34
01:44.700 --> 01:48.500
So the module cannot work unless you use this particular parameter.

35
01:48.500 --> 01:51.710
So realistically, if we scroll on down, we can see all of these additional

36
01:51.710 --> 01:52.700
examples right here.

37
01:52.700 --> 01:56.220
And feel free to copy them, paste them, try them out and see how you get on.

38
01:56.220 --> 01:59.500
But from now, let's write one of our own, which is really quite simple.

39
01:59.500 --> 02:03.220
So let's go back and we'll create a playbook called Play4.yml
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

39
01:59.500 --> 02:03.220
So let's go back and we'll create a playbook called Play4.yml

40
02:03.220 --> 02:07.050
and go into Incent Mode, have our three hyphens at the top, give the playbook a

41
02:07.050 --> 02:07.300
name,

42
02:07.300 --> 02:10.500
and I'll just say Play4, testing the asset module.

43
02:10.500 --> 02:13.690
So the host I'm going to target will be the all group, and again, we'll set

44
02:13.690 --> 02:14.580
gather_facts to false.

45
02:14.580 --> 02:16.100
We still don't need these facts.

46
02:16.100 --> 02:18.300
And then we will specify our task.

47
02:18.300 --> 02:20.180
Now the very first task is going to have a name.

48
02:20.180 --> 02:23.700
And let's just say, test whether a file exists.

49
02:23.700 --> 02:27.080
Now if you recall earlier on within this course, we happen to use the stat

50
02:27.080 --> 02:27.700
module.

51
02:27.700 --> 02:30.420
Again, this is built in, we don't have to install anything.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

51
02:27.700 --> 02:30.420
Again, this is built in, we don't have to install anything.

52
02:30.420 --> 02:34.700
And pretty much, this is going to allow us to determine whether a file exists,

53
02:34.700 --> 02:38.420
as well as a whole bunch of other information, such as as the file directory,

54
02:38.420 --> 02:40.500
what is the size of the file that type of thing.

55
02:40.500 --> 02:42.620
So we can use this to perform a very simple test.

56
02:42.620 --> 02:44.260
Let's go back to our playbook then.

57
02:44.260 --> 02:49.220
And the module we will use will be Ansible.bilton.stat.

58
02:49.220 --> 02:52.100
Now in this case here, we're going to supply a particular path.

59
02:52.100 --> 02:54.740
So what I will do here is I'll go into my blue box.

60
02:54.740 --> 02:56.980
So let's go into the 10th directory, do an LS.

61
02:56.980 --> 03:00.260
If I just create a little file here, let's call this, or I don't know.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

61
02:56.980 --> 03:00.260
If I just create a little file here, let's call this, or I don't know.

62
03:00.260 --> 03:04.900
Let's just say recipe.txt, go into insert mode, and we just add in some values,

63
03:04.900 --> 03:06.500
check in potatoes and veg.

64
03:06.500 --> 03:10.260
And crucially, we won't have this particular file over on the yellow box.

65
03:10.260 --> 03:15.060
So let's use Ansible to detect the presence of that file across our devices.

66
03:15.060 --> 03:18.100
So what I will do here is specify the path that I want to check.

67
03:18.100 --> 03:21.620
I will say forward slash TMP, and then it's going to be recipe.

68
03:21.620 --> 03:25.580
And I want to capture the output as a variables, let's just call this, recipe

69
03:25.580 --> 03:25.940
output.

70
03:25.940 --> 03:29.620
And now I'll have a new task with the name just specifying a set, whether the

71
03:29.620 --> 03:31.540
recipe file exists.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

71
03:29.620 --> 03:31.540
recipe file exists.

72
03:31.540 --> 03:34.980
So to specify the Ansible built in a set module right here.

73
03:34.980 --> 03:38.500
Now remember, the required parameter is the keyword that.

74
03:38.500 --> 03:41.060
And now I want to grab my variable here.

75
03:41.060 --> 03:45.730
And because I have used the STAT module, if your recall, this is going to bind

76
03:45.730 --> 03:45.860
some

77
03:45.860 --> 03:49.140
particular attributes to the actual variable itself.

78
03:49.140 --> 03:52.580
So we can go into the STAT attribute and then we should also have an attribute

79
03:52.580 --> 03:53.140
known as

80
03:53.140 --> 03:54.020
exists.

81
03:54.020 --> 03:57.540
And now here very elegantly, we can specify our success message.

82
03:57.540 --> 04:02.820
And thank rotation marks, I'll just say the recipe.txt file exists on.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

82
03:57.540 --> 04:02.820
And thank rotation marks, I'll just say the recipe.txt file exists on.

83
04:02.820 --> 04:05.060
And I'll specify my inventory host name.

84
04:05.060 --> 04:07.940
And I'll also have my failure message.

85
04:07.940 --> 04:10.660
So specify fail underscore MSG.

86
04:10.660 --> 04:15.540
And with the rotation marks, I'll say the recipe.txt file does not exist.

87
04:15.540 --> 04:18.340
And again, I'll pass in my inventory host name.

88
04:18.340 --> 04:19.460
So fairly simple test.

89
04:19.460 --> 04:20.660
And it is very readable.

90
04:20.660 --> 04:24.260
So if I go back to my terminal run my playbook of playbook 4.YML,

91
04:24.260 --> 04:28.260
we should see a result specifying that the blue box does have this fail,

92
04:28.260 --> 04:30.020
whereas the yellow box does not.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

92
04:28.260 --> 04:30.020
whereas the yellow box does not.

93
04:30.020 --> 04:31.700
So let's try and hit enter.

94
04:31.700 --> 04:32.820
And the playbook runs now.

95
04:32.820 --> 04:33.460
Look at the output here.

96
04:33.460 --> 04:34.820
This is really, really handy.

97
04:34.820 --> 04:36.260
We get to see our message.

98
04:36.260 --> 04:40.070
And the case of the blue box here, it specifies the recipe.txt file exists in

99
04:40.070 --> 04:40.580
the blue box.

100
04:40.580 --> 04:44.180
But more interestingly, is that when we happen to have a failure,

101
04:44.180 --> 04:46.260
we then just print out a fail message.

102
04:46.260 --> 04:49.700
We actually instructed Ansible about a genuine failure.

103
04:49.700 --> 04:50.260
Look at this.

104
04:50.260 --> 04:52.020
Ansible reporting a failure.

105
04:52.020 --> 04:56.820
And in our play recap, we can actually see we've updated a failure right here.

106
04:56.820 --> 04:58.980
You see that one failure is present.

107
04:58.980 --> 05:02.500
This is also one of the advantages of using this particular module.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

107
04:58.980 --> 05:02.500
This is also one of the advantages of using this particular module.

108
05:02.500 --> 05:06.820
As opposed to you just printing out a message saying, hey, the test field,

109
05:06.820 --> 05:10.900
and effectively it's not really updated in the Ansible output here.

110
05:10.900 --> 05:14.020
So by using the assert module, we get that update,

111
05:14.020 --> 05:18.100
and we get this nice clean way to specify whether a test passed,

112
05:18.100 --> 05:21.060
or whether in this case here a test happened to fail.

113
05:21.060 --> 05:24.740
So with all that said, let's now validate what we've learned within this scale.
