WEBVTT

1
00:00.000 --> 00:05.230
So friends, previously we just discussed the concept of loops within Ansible

2
00:05.230 --> 00:05.840
and we learned

3
00:05.840 --> 00:10.140
that this was a core component of programming. What I now want to do is to talk

4
00:10.140 --> 00:10.560
to you about

5
00:10.560 --> 00:14.360
yet another very important subject related to programming and that is

6
00:14.360 --> 00:15.680
conditional statements.

7
00:15.680 --> 00:20.720
So with that said, let's just dive in. So we are really digging deep into the

8
00:20.720 --> 00:21.440
components of

9
00:21.440 --> 00:25.280
programming right now. The good news is, is it with respect to conditionals

10
00:25.280 --> 00:27.040
much like with loops.

11
00:27.040 --> 00:31.040
Whilst we are ultimately using the Python programming language under the hood
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:27.040 --> 00:31.040
Whilst we are ultimately using the Python programming language under the hood

12
00:31.040 --> 00:32.080
via Ansible,

13
00:32.080 --> 00:36.560
the way this particular concept is exposed to us as an end user of Ansible is

14
00:36.560 --> 00:37.200
once again

15
00:37.200 --> 00:42.240
through a greatly simplified abstraction layer. Nevertheless, it is a good idea

16
00:42.240 --> 00:43.040
to understand

17
00:43.040 --> 00:46.880
what exactly is going on under the hood so that we can correctly apply it

18
00:46.880 --> 00:48.480
within our playbooks.

19
00:48.480 --> 00:52.560
So let's talk about what a conditional statement actually is and why we would

20
00:52.560 --> 00:53.360
want to use it.

21
00:53.360 --> 00:56.790
Now the first thing to say is that this is a very, very important subject. So

22
00:56.790 --> 00:57.600
we definitely

23
00:57.600 --> 01:01.600
want to be paying attention because if you don't pay attention, then you're not
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:57.600 --> 01:01.600
want to be paying attention because if you don't pay attention, then you're not

24
01:01.600 --> 01:02.080
going to be able to

25
01:02.080 --> 01:06.480
use them with 10 your playbooks. Okay, so confession. What I actually did there

26
01:06.480 --> 01:08.000
was give you an example

27
01:08.000 --> 01:12.800
of a conditional statement. Now in the world of Python programming, the way we

28
01:12.800 --> 01:14.000
define a conditional

29
01:14.000 --> 01:18.220
statement is using the keyword if basically what we're actually asking is a

30
01:18.220 --> 01:19.120
billion question.

31
01:19.120 --> 01:23.030
Now recall we talked about billions that is if something is either true or

32
01:23.030 --> 01:24.960
something is false.

33
01:24.960 --> 01:28.940
It is one or the other. It's a light switch, not a dimmer. So let me give you a

34
01:28.940 --> 01:30.320
very simple example.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:28.940 --> 01:30.320
very simple example.

35
01:30.320 --> 01:36.720
If you walk into Starbucks and you pay for a coffee, then something is going to

36
01:36.720 --> 01:37.200
happen.

37
01:37.200 --> 01:41.280
And in this case here, what's going to happen is you're going to get a coffee.

38
01:41.280 --> 01:42.720
So this part here,

39
01:42.720 --> 01:46.640
this is actually the condition that we had to meet. And this is the part we're

40
01:46.640 --> 01:47.760
evaluating on a

41
01:47.760 --> 01:52.160
billing condition. Is this true or is this false? And if what we evaluate

42
01:52.160 --> 01:54.320
becomes true, we specify

43
01:54.320 --> 01:58.970
what is going to happen as a consequence. So ask yourself this, if you pay for

44
01:58.970 --> 01:59.840
a coffee, and this

45
01:59.840 --> 02:04.330
is true, I you really did pay for a coffee, then this happens as a consequence,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

45
01:59.840 --> 02:04.330
is true, I you really did pay for a coffee, then this happens as a consequence,

46
02:04.330 --> 02:05.440
you actually get a

47
02:05.440 --> 02:10.410
coffee. Now conversely, if I evaluate this, and it comes to false, so basically

48
02:10.410 --> 02:11.280
I say, if you pay

49
02:11.280 --> 02:16.230
for a coffee, and this is false, I you did not pay for a coffee, the condition

50
02:16.230 --> 02:17.280
below is actually

51
02:17.280 --> 02:21.080
not going to happen, you're not going to get a coffee. Now this might seem

52
02:21.080 --> 02:22.080
almost a trivial

53
02:22.080 --> 02:25.600
example, but this is really how programming logic actually works. It basically

54
02:25.600 --> 02:26.240
breaks down

55
02:26.240 --> 02:30.390
into a series of if statements or conditional statements. Now as we're going to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

55
02:26.240 --> 02:30.390
into a series of if statements or conditional statements. Now as we're going to

56
02:30.390 --> 02:30.800
learn my

57
02:30.800 --> 02:34.160
perspective answer will be actually structure this a little bit differently.

58
02:34.160 --> 02:35.440
What I'm doing here

59
02:35.440 --> 02:39.680
is actually representing the underlying Python. So we don't have to worry about

60
02:39.680 --> 02:40.320
these particular

61
02:40.320 --> 02:44.410
keywords, but they are actually informative to really understand what we're

62
02:44.410 --> 02:45.600
trying to do here.

63
02:45.600 --> 02:49.200
So let's take another example. So let's say we happen to have multiple

64
02:49.200 --> 02:50.880
conditions, we can therefore

65
02:50.880 --> 02:55.990
ultimately specify multiple outcomes or multiple consequences. So let's use the

66
02:55.990 --> 02:56.880
example of the

67
02:56.880 --> 03:01.820
United States and the US, you have to be 21 years of age or older to buy
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:56.880 --> 03:01.820
United States and the US, you have to be 21 years of age or older to buy

68
03:01.820 --> 03:03.600
alcohol. Whereas if you

69
03:03.600 --> 03:09.340
happen to be 18, you can vote, but you can't buy alcohol. And if you happen to

70
03:09.340 --> 03:10.800
be less than 18,

71
03:10.800 --> 03:14.560
you can't vote or you can't buy alcohol. So really the way we would map this

72
03:14.560 --> 03:15.920
would be the question

73
03:15.920 --> 03:23.240
if you happen to be 21 or over, then as a consequence, you can buy beer and you

74
03:23.240 --> 03:25.120
can vote. But let's say

75
03:25.120 --> 03:28.890
you don't meet that condition, we can have another one to cover it. And by the

76
03:28.890 --> 03:30.000
way, just to be clear
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

77
03:30.000 --> 03:33.920
here, this is not a valid Python code, as you may suggest. This is just an

78
03:33.920 --> 03:35.200
example of pseudocode

79
03:35.200 --> 03:40.750
to help you understand the concept. So let's say you are 18 or over, then you

80
03:40.750 --> 03:41.920
cannot buy beer,

81
03:41.920 --> 03:46.610
but you can vote. And if you happen to be less than 18, then we have another

82
03:46.610 --> 03:48.000
condition, which is

83
03:48.000 --> 03:53.040
no beer or voting. At its core, this is exactly what we're talking about. So if

84
03:53.040 --> 03:54.080
you had a program

85
03:54.080 --> 03:59.600
that tested this logic and someone come up who was 25 years old, if they are 21

86
03:59.600 --> 04:00.640
or above, that is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

86
03:59.600 --> 04:00.640
or above, that is

87
04:00.640 --> 04:04.740
the condition we're testing, or that would actually be true, 25 is greater than

88
04:04.740 --> 04:06.560
21. Then as a result,

89
04:06.560 --> 04:10.320
they would be allowed to buy beer and they could also vote. Whereas conversely,

90
04:10.320 --> 04:11.200
if someone who was

91
04:11.200 --> 04:15.570
19 years old came up and used a program, this part here would actually evaluate

92
04:15.570 --> 04:16.080
to false,

93
04:16.080 --> 04:20.690
because they're not 21 or over there for this part here would not be applicable

94
04:20.690 --> 04:21.520
. However,

95
04:21.520 --> 04:25.520
we could then jump to the next condition and ask them, are you 18 or over? Well

96
04:25.520 --> 04:26.720
, in that case,

97
04:26.720 --> 04:31.960
the condition is indeed true. So because of that, the consequence here, then
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

97
04:26.720 --> 04:31.960
the condition is indeed true. So because of that, the consequence here, then

98
04:31.960 --> 04:33.520
you cannot buy beer,

99
04:33.520 --> 04:37.980
but you can vote. And then lastly, if someone came up who happened to be 15

100
04:37.980 --> 04:39.360
years old, we could test

101
04:39.360 --> 04:43.370
the condition here. If they are over 21, that would be false. So this part here

102
04:43.370 --> 04:44.560
would not happen.

103
04:44.560 --> 04:49.040
Are they 18 or over? Well, this is also false. So once again, this part here

104
04:49.040 --> 04:50.320
would also not happen.

105
04:50.320 --> 04:53.280
And then they go to the very last condition, if they're under 18, well, that

106
04:53.280 --> 04:54.400
happened to be true.

107
04:54.400 --> 04:59.120
So then we specify what is going to happen, no beer or voting for you. So those

108
04:59.120 --> 05:00.000
are some examples
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

109
05:00.000 --> 05:03.820
of conditional logic in the real world. But as you're going to learn, this can

110
05:03.820 --> 05:04.480
be very, very

111
05:04.480 --> 05:08.540
instructive and useful when dealing with automation. Take for example, we

112
05:08.540 --> 05:10.080
happen to have this device

113
05:10.080 --> 05:13.910
here, which happens to be a red heart machine. And similarly here, we have a

114
05:13.910 --> 05:14.960
device which happened

115
05:14.960 --> 05:19.250
to be a Debian based machine. Let's maybe say it's Ubuntu. If we are managing

116
05:19.250 --> 05:19.760
both of these

117
05:19.760 --> 05:24.440
devices via Ansible, well, the way we actually interact with each device is

118
05:24.440 --> 05:25.200
going to be a little

119
05:25.200 --> 05:29.170
bit different depending on the operating system. Say for example, you want to

120
05:29.170 --> 05:30.240
perform an update of
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

120
05:29.170 --> 05:30.240
perform an update of

121
05:30.240 --> 05:34.150
the system, but on a red heart based machine, the package management utility is

122
05:34.150 --> 05:35.680
going to be DNF.

123
05:35.680 --> 05:41.720
So basically, if you are a red heart based machine, then you should use DNF.

124
05:41.720 --> 05:42.560
However,

125
05:42.560 --> 05:47.220
if you happen to be a Debian based machine, then you should use the apt package

126
05:47.220 --> 05:47.840
manager.

127
05:47.840 --> 05:52.000
This is an example of using conditional logic to manage the differences across

128
05:52.000 --> 05:52.720
devices within

129
05:52.720 --> 05:56.410
our inventory, because say for example, if I happen to be on a red heart based

130
05:56.410 --> 05:57.040
machine,

131
05:57.040 --> 06:01.520
which as we can see here, we are on Red Hat Enterprise Linux. If I try to use
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

131
05:57.040 --> 06:01.520
which as we can see here, we are on Red Hat Enterprise Linux. If I try to use

132
06:01.520 --> 06:02.240
the Debian

133
06:02.240 --> 06:06.640
package manager in Sudo, apt updates and to answer, we can see here the command

134
06:06.640 --> 06:07.440
is not found. So if

135
06:07.440 --> 06:10.880
we happen to push out this command automatically to all devices that are

136
06:10.880 --> 06:12.240
inventory, the ones that

137
06:12.240 --> 06:16.620
do not happen to be Debian based are not going to operate. The devices such as

138
06:16.620 --> 06:17.280
this one here,

139
06:17.280 --> 06:22.640
the rail device, for those devices, we would have to use Sudo DNF update. And

140
06:22.640 --> 06:23.680
if we hit enter,

141
06:23.680 --> 06:27.410
suddenly we can update our package manager. And it's going to be using this

142
06:27.410 --> 06:28.320
conditional logic

143
06:28.320 --> 06:31.890
that's going to allow us to control this behavior. Now, just like I said, when
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
06:28.320 --> 06:31.890
that's going to allow us to control this behavior. Now, just like I said, when

144
06:31.890 --> 06:33.040
we use the F keyword,

145
06:33.040 --> 06:37.230
this is actually a Python construct. When it comes to utilizing Ansible via the

146
06:37.230 --> 06:38.560
abstraction layer,

147
06:38.560 --> 06:42.770
we actually use a different keyword. Let's find out what that is in the very

148
06:42.770 --> 06:43.440
next video.
