WEBVTT

1
00:00.000 --> 00:03.880
Okay friends, so now that we understand the general logic behind the

2
00:03.880 --> 00:05.200
conditional statement,

3
00:05.200 --> 00:09.040
let's now see how Ansible actually implements these conditional statements.

4
00:09.040 --> 00:12.320
So let's go directly to the Ansible documentation.

5
00:12.320 --> 00:16.720
So let's go to the big Google machine and if you just search for Ansible

6
00:16.720 --> 00:17.680
conditionals,

7
00:17.680 --> 00:21.040
you will be taken to this particular link here. So let's click on this.

8
00:21.040 --> 00:25.120
So it says here in a playbook, you may want to execute different tasks or have

9
00:25.120 --> 00:25.520
different

10
00:25.520 --> 00:30.110
goals depending on the value of a fact, a variable or the result of a previous
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

10
00:25.520 --> 00:30.110
goals depending on the value of a fact, a variable or the result of a previous

11
00:30.110 --> 00:30.480
task.

12
00:30.480 --> 00:34.640
Basically, at its core, all we're seeing here is we may want to do different

13
00:34.640 --> 00:35.200
things

14
00:35.200 --> 00:38.960
depending on certain conditions. Now we can see here that Ansible uses

15
00:38.960 --> 00:39.840
something known as

16
00:39.840 --> 00:44.240
Ginger2Tests and filters, we'll talk about that in a later scale. But for now,

17
00:44.240 --> 00:44.800
what we are going

18
00:44.800 --> 00:50.400
to focus in on is this part here, basic conditionals with when. Quite simply,

19
00:50.400 --> 00:51.760
the when keyword is

20
00:51.760 --> 00:56.130
going to allow us to specify when something is true, it really kind of similar

21
00:56.130 --> 00:56.880
to what we talked

22
00:56.880 --> 01:01.600
about in the previous video, when we asked the question if something is true.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

22
00:56.880 --> 01:01.600
about in the previous video, when we asked the question if something is true.

23
01:01.600 --> 01:02.240
So let's just look

24
01:02.240 --> 01:06.760
at the documentation here to see some examples. Let's maybe go to conditions

25
01:06.760 --> 01:07.920
based on variables.

26
01:07.920 --> 01:11.130
So we can see we have a really silly but fun example here within the Ansible

27
01:11.130 --> 01:12.160
docs, we have a

28
01:12.160 --> 01:17.200
variable with a key called epic and the corresponding value true. We also have

29
01:17.200 --> 01:18.720
a key called monumental

30
01:18.720 --> 01:22.560
and the corresponding value. Yes. So let's look at the task. Now the first one

31
01:22.560 --> 01:24.320
here happens to use

32
01:24.320 --> 01:27.820
multiple conditions. Let's just forget about that for the moment. Let's focus

33
01:27.820 --> 01:29.040
on this one here.

34
01:29.040 --> 01:35.100
The name of the task is run the command if epic happens to be false. So all we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:29.040 --> 01:35.100
The name of the task is run the command if epic happens to be false. So all we

35
01:35.100 --> 01:35.600
're going to do

36
01:35.600 --> 01:39.390
here is to use the Ansible built in shell module to accurate the command. This

37
01:39.390 --> 01:40.720
certainly is an epic

38
01:40.720 --> 01:44.530
only when we have a particular condition. Now in the case here, as per the

39
01:44.530 --> 01:45.680
example, we actually

40
01:45.680 --> 01:50.080
have some type of inversion, we say not epic, which is pretty much just syntax

41
01:50.080 --> 01:50.720
specifying

42
01:50.720 --> 01:55.060
that the epic value should be false. Now right now the epic value is set to

43
01:55.060 --> 01:56.160
true. And as such,

44
01:56.160 --> 02:00.960
this particular condition here doesn't actually match the condition specified
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

44
01:56.160 --> 02:00.960
this particular condition here doesn't actually match the condition specified

45
02:00.960 --> 02:02.160
within the variable.

46
02:02.160 --> 02:06.960
Therefore this particular shell command is not going to execute. So it's really

47
02:06.960 --> 02:07.760
all about the use

48
02:07.760 --> 02:11.430
of this when keyword and specifying a particular condition, which is going to

49
02:11.430 --> 02:12.240
inform what is going

50
02:12.240 --> 02:16.640
to happen with the task specifying the condition. Now just below we saw this

51
02:16.640 --> 02:18.560
use of the word not

52
02:18.560 --> 02:23.400
and correspondence with the condition epic. What we actually saw here was a

53
02:23.400 --> 02:24.640
logical operator in

54
02:24.640 --> 02:30.210
use. So the logical operators are the and operator, the or operator. And as we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

54
02:24.640 --> 02:30.210
use. So the logical operators are the and operator, the or operator. And as we

55
02:30.210 --> 02:31.520
just saw there,

56
02:31.520 --> 02:36.520
the not operator. Now by default, when we have multiple conditions, the logical

57
02:36.520 --> 02:37.200
operator at

58
02:37.200 --> 02:41.120
play that is going to be assumed as the and operator. And the way you pass this

59
02:41.120 --> 02:42.160
to Ansible is in the

60
02:42.160 --> 02:46.390
form of a list. So let's take this particular example that we have right here.

61
02:46.390 --> 02:47.200
Okay. So the

62
02:47.200 --> 02:50.790
name of the task is to shut down sent to us six systems. So this is something

63
02:50.790 --> 02:52.000
we're testing for

64
02:52.000 --> 02:57.940
very, very explicitly. It has to be a sent to a system. And it also has to have

65
02:57.940 --> 02:58.960
version six.

66
02:58.960 --> 03:03.120
See that both of these have to be true. So let's look at the actual condition
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

66
02:58.960 --> 03:03.120
See that both of these have to be true. So let's look at the actual condition

67
03:03.120 --> 03:04.240
here as specified

68
03:04.240 --> 03:07.810
via the when keyword once again, as highlighted in the documentation, we

69
03:07.810 --> 03:09.440
specify this via a list.

70
03:09.440 --> 03:13.160
So here we have it right here and array object. So the conditions that have to

71
03:13.160 --> 03:13.840
be true here

72
03:13.840 --> 03:18.390
is the answerable distribution. And don't worry about this syntax. Ansible

73
03:18.390 --> 03:18.480
facts,

74
03:18.480 --> 03:21.920
we'll talk about that later on. This is just simply a way to gather information

75
03:21.920 --> 03:22.640
about an

76
03:22.640 --> 03:26.450
Ansible system. So in this case here, we're just testing the distribution. If

77
03:26.450 --> 03:27.360
that returns the

78
03:27.360 --> 03:31.190
values sent to us, that is one condition. And because this is true, we can now
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

78
03:27.360 --> 03:31.190
values sent to us, that is one condition. And because this is true, we can now

79
03:31.190 --> 03:32.080
test the next

80
03:32.080 --> 03:38.400
condition, which is as the distribution major version equal to the value six,

81
03:38.400 --> 03:39.840
if that is also true,

82
03:39.840 --> 03:43.620
then as a consequence, we have passed the test, because in this case here, we

83
03:43.620 --> 03:45.280
have the and operator,

84
03:45.280 --> 03:50.130
this has to be true. And this has to be true. And when that happens, what we're

85
03:50.130 --> 03:51.040
going to do is

86
03:51.040 --> 03:56.890
send the shutdown dash T no command to any devices that match that criteria.

87
03:56.890 --> 03:57.680
And essence,

88
03:57.680 --> 04:02.480
shutting down all sent to us version six systems. So let's take a look at a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

88
03:57.680 --> 04:02.480
shutting down all sent to us version six systems. So let's take a look at a

89
04:02.480 --> 04:03.520
simple example. Then,

90
04:03.520 --> 04:06.800
okay. So like I say, a classic example to use with respect to a conditional

91
04:06.800 --> 04:07.520
statement is to

92
04:07.520 --> 04:11.540
highlight the differences in package management across Linux systems. We use D

93
04:11.540 --> 04:12.400
NF for Red Hat

94
04:12.400 --> 04:16.380
devices, and we would use apt for the Debian based devices. Now, as it trans

95
04:16.380 --> 04:17.920
pires, both of the

96
04:17.920 --> 04:21.930
devices in our inventory happen to be Red Hat enterprise Linux. Therefore, they

97
04:21.930 --> 04:23.040
both use DNF.

98
04:23.040 --> 04:26.780
So we're not going to use this as an example. What I'm going to do is I'm going

99
04:26.780 --> 04:27.520
to go into my

100
04:27.520 --> 04:31.400
inventory, and I'm going to add in some additional groups. So let's imagine we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

100
04:27.520 --> 04:31.400
inventory, and I'm going to add in some additional groups. So let's imagine we

101
04:31.400 --> 04:32.560
have a UK group,

102
04:32.560 --> 04:37.120
and imagine that the blue box is located in the United Kingdom. And similarly,

103
04:37.120 --> 04:38.080
let's have a USA

104
04:38.080 --> 04:42.000
group. And let's imagine that yellow box resides in the USA. Now, of course,

105
04:42.000 --> 04:42.960
that says very, very

106
04:42.960 --> 04:46.760
trivial. We only have two devices, one of each in each group. But you could

107
04:46.760 --> 04:47.840
easily imagine if we

108
04:47.840 --> 04:53.040
happen to have thousands of devices, maybe say purple box and green box. They

109
04:53.040 --> 04:54.080
could be in the USA.

110
04:54.080 --> 04:57.980
Whereas in the UK, we can have blue box, pink box and a whole bunch of others.

111
04:57.980 --> 04:58.560
You kind of get

112
04:58.560 --> 05:01.490
the drift. We don't have to be limited to one device per group. But in this
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

112
04:58.560 --> 05:01.490
the drift. We don't have to be limited to one device per group. But in this

113
05:01.490 --> 05:02.240
case here, we only

114
05:02.240 --> 05:05.680
have two devices to manage. So let's just write out the changes. Here we can

115
05:05.680 --> 05:06.400
see we have now

116
05:06.400 --> 05:10.960
updated the inventory. What I want to do is I want to test it via a conditional

117
05:10.960 --> 05:11.440
statement. So

118
05:11.440 --> 05:14.770
let's just call this condition dot yml. If I go into insert mode, have my three

119
05:14.770 --> 05:15.920
items at the top,

120
05:15.920 --> 05:20.770
give my play a name, creating files based on location. So what I'm going to do

121
05:20.770 --> 05:21.600
is I'm going to target

122
05:21.600 --> 05:25.630
all of my devices. And I will specify the tasks that I want to happen. So the

123
05:25.630 --> 05:26.880
name of the task

124
05:26.880 --> 05:32.390
is going to be create UK file for UK devices. And I'm going to use the Ansible
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

124
05:26.880 --> 05:32.390
is going to be create UK file for UK devices. And I'm going to use the Ansible

125
05:32.390 --> 05:34.160
built in shell module.

126
05:34.160 --> 05:39.680
And all I'm going to do is say echo, I am a UK device. And I'm going to

127
05:39.680 --> 05:41.280
redirect that

128
05:41.280 --> 05:45.640
into the 10th directory. And I'll put it into a file called location dot txt.

129
05:45.640 --> 05:46.480
Now similarly,

130
05:46.480 --> 05:50.240
I'm going to have another one, a rather another task. And I'll just say create

131
05:50.240 --> 05:53.360
USA file for USA

132
05:53.360 --> 05:57.100
devices. Similarly, we're going to use the same module Ansible built in shell.

133
05:57.100 --> 05:57.360
And we're just

134
05:57.360 --> 06:03.130
going to echo this time. I am a USA device and redirect that into temp location
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

134
05:57.360 --> 06:03.130
going to echo this time. I am a USA device and redirect that into temp location

135
06:03.130 --> 06:04.320
once again.

136
06:04.320 --> 06:07.760
Now the crucial thing to note here is that we don't want this to happen for

137
06:07.760 --> 06:08.640
both devices as

138
06:08.640 --> 06:12.390
it's going to happen right now. So here is the condition that I'm going to test

139
06:12.390 --> 06:12.960
. I'm going to use

140
06:12.960 --> 06:18.940
the condition when I'm going to have the keyword UK. And I'm going to specify

141
06:18.940 --> 06:20.720
that when we have UK

142
06:20.720 --> 06:24.860
inside of the group underscore names. Now where on earth am I getting group

143
06:24.860 --> 06:25.680
underscore names from?

144
06:25.680 --> 06:29.690
That just happens to be what is known as a magic variable. Basically, this is

145
06:29.690 --> 06:30.240
just one of the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

145
06:29.690 --> 06:30.240
just one of the

146
06:30.240 --> 06:33.650
special variables that Ansible comes shipped with that holds particular

147
06:33.650 --> 06:34.880
information about the

148
06:34.880 --> 06:38.630
device within the inventory. Now we'll talk more about magic variables later on

149
06:38.630 --> 06:39.760
. So definitely

150
06:39.760 --> 06:42.750
don't get worried thinking I would never known to use that particular magic

151
06:42.750 --> 06:43.760
variable. So all I'm

152
06:43.760 --> 06:47.780
doing here is looking at all the groups that the device happens to be a member

153
06:47.780 --> 06:48.720
of. And if there

154
06:48.720 --> 06:52.350
happens to be a group called UK in the list of groups to which they are a

155
06:52.350 --> 06:53.680
member, that condition

156
06:53.680 --> 06:57.730
therefore would be true. And as a consequence, we are going to run this

157
06:57.730 --> 06:59.280
particular shell command.

158
06:59.280 --> 07:04.400
I am a UK device and redirect into location.txt. So if you happen to have a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

158
06:59.280 --> 07:04.400
I am a UK device and redirect into location.txt. So if you happen to have a

159
07:04.400 --> 07:06.000
device in the USA,

160
07:06.000 --> 07:11.290
which is not in the UK group for that particular device, this condition here is

161
07:11.290 --> 07:12.160
going to be false.

162
07:12.160 --> 07:16.960
And when it's false, that means that the command above, i.e. echo, I am a UK

163
07:16.960 --> 07:18.240
device is not going

164
07:18.240 --> 07:22.020
to execute. Now we also want to do a similar thing for the below task. So we'll

165
07:22.020 --> 07:23.600
add in the when keyword.

166
07:23.600 --> 07:27.030
Again, we'll have our double quotation marks with single quotation marks

167
07:27.030 --> 07:27.920
contained within

168
07:27.920 --> 07:33.050
specifying USA once again in the group names, magic variable. So therefore,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

168
07:27.920 --> 07:33.050
specifying USA once again in the group names, magic variable. So therefore,

169
07:33.050 --> 07:33.840
this particular

170
07:33.840 --> 07:39.040
command is only going to execute for devices that belong to the USA group. Okay

171
07:39.040 --> 07:40.640
, so let's just

172
07:40.640 --> 07:45.680
write and quit. If I just say answerable playbook condition dot yml and to

173
07:45.680 --> 07:46.160
enter

174
07:46.160 --> 07:50.680
answerable executes. And as it transpires, answerable is actually skipped

175
07:50.680 --> 07:51.840
absolutely everything. So

176
07:51.840 --> 07:55.190
that wasn't supposed to happen, but I know exactly why it did happen. And this

177
07:55.190 --> 07:55.920
is quite good actually.

178
07:55.920 --> 08:00.650
This is very, very important. We can see here for the task create UK file for
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

178
07:55.920 --> 08:00.650
This is very, very important. We can see here for the task create UK file for

179
08:00.650 --> 08:01.440
UK devices.

180
08:01.440 --> 08:06.470
Answerable has skipped both the yellow box and the blue box. And for the next

181
08:06.470 --> 08:08.160
task, create a USA

182
08:08.160 --> 08:12.530
file for USA devices. Answerable once again has skipped both devices. Pretty

183
08:12.530 --> 08:13.520
much answerable has

184
08:13.520 --> 08:17.770
denoted that the when condition in both cases happened to be false. Now why

185
08:17.770 --> 08:18.720
exactly is that?

186
08:18.720 --> 08:22.640
Well, let's just look briefly at the inventory once again, if we cat the file.

187
08:22.640 --> 08:27.680
Check this out. The UK group happens to be UK in lower case and the USA group

188
08:27.680 --> 08:28.320
happens to be in

189
08:28.320 --> 08:32.070
lower case as well. Whereas if we look at the actual conditions as specified
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

189
08:28.320 --> 08:32.070
lower case as well. Whereas if we look at the actual conditions as specified

190
08:32.070 --> 08:33.120
via the answerable

191
08:33.120 --> 08:37.660
playbook, the condition here is USA in capital letters and UK in capital

192
08:37.660 --> 08:39.040
letters. So pretty much

193
08:39.040 --> 08:42.840
we don't have a capital letter UK group. So this is false for both devices and

194
08:42.840 --> 08:43.680
we don't have a

195
08:43.680 --> 08:47.840
capital letter USA group for both devices. So it's going to be skipped twice.

196
08:47.840 --> 08:51.680
So let's just briefly fix this. And if I go into insert mode and I'll just

197
08:51.680 --> 08:52.560
change this to

198
08:52.560 --> 08:57.120
lower case, scroll on down and also change USA to lower case. Let's just exit

199
08:57.120 --> 08:57.360
to the

200
08:57.360 --> 09:01.840
scape, right and quit, clear the screen, arrow up and let's rerun the playbook.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

200
08:57.360 --> 09:01.840
scape, right and quit, clear the screen, arrow up and let's rerun the playbook.

201
09:01.840 --> 09:06.710
Aha, this is much better this time to look at the output here. It says for the

202
09:06.710 --> 09:08.080
task creating UK

203
09:08.080 --> 09:13.760
file for UK devices, it has skipped the yellow box but it changed the blue box.

204
09:13.760 --> 09:14.080
That's because

205
09:14.080 --> 09:17.920
the blue box happened to be a member of the UK group. Whereas when you go down

206
09:17.920 --> 09:19.200
to the next task,

207
09:19.200 --> 09:24.400
create a USA file for the USA devices, the inverse is true. We have skipped the

208
09:24.400 --> 09:25.280
blue box,

209
09:25.280 --> 09:29.560
whereas we have changed the yellow box again based on the conditions. So if we

210
09:29.560 --> 09:30.160
go to say,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

210
09:29.560 --> 09:30.160
go to say,

211
09:30.160 --> 09:34.790
for example, the blue box, we should have a UK file as specified like a say in

212
09:34.790 --> 09:35.760
our inventory,

213
09:35.760 --> 09:40.050
blue box is UK, yellow box is USA. So and say the blue box, we go to the 10th

214
09:40.050 --> 09:40.400
directory,

215
09:40.400 --> 09:45.120
we have this file called location dot txt. If we cat this fail, it says here, I

216
09:45.120 --> 09:46.240
am a UK device,

217
09:46.240 --> 09:50.390
as expected. Let's go to yellow box here, we see the enter the 10 directory, do

218
09:50.390 --> 09:50.800
an LS,

219
09:50.800 --> 09:54.610
we have the location fail, let's cat this fail and as we can see here, for the

220
09:54.610 --> 09:55.280
yellow box,

221
09:55.280 --> 09:59.040
we have applied the text, I am a USA device.
