WEBVTT

1
00:00.000 --> 00:04.240
So friends in the previous video we discussed how we can utilize loops within

2
00:04.240 --> 00:05.680
our Gen 2 templates.

3
00:05.680 --> 00:09.920
What we now want to do is to see how we can use conditional logic and how we're

4
00:09.920 --> 00:10.400
going to learn

5
00:10.400 --> 00:14.800
how to do that. Well the best place to go as always is to the documentation so

6
00:14.800 --> 00:15.120
that's what

7
00:15.120 --> 00:19.000
we're going to do. So again if we just happen to search for Gen 2 documentation

8
00:19.000 --> 00:19.760
let's click this

9
00:19.760 --> 00:24.440
top link right here and this time we're going to once again go to our control

10
00:24.440 --> 00:26.080
structures right here

11
00:26.080 --> 00:29.650
and if we just scroll on down a little bit we're going to come to this part

12
00:29.650 --> 00:30.560
right here. So what
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

12
00:29.650 --> 00:30.560
right here. So what

13
00:30.560 --> 00:34.430
we actually see here is the F statement. Now it says here that F statement in

14
00:34.430 --> 00:36.000
Ginger is comparable

15
00:36.000 --> 00:40.190
with the F statement in Python. So once again if you are familiar with Python

16
00:40.190 --> 00:41.280
you'll have a big

17
00:41.280 --> 00:45.470
advantage with respect to these Gen 2 templates. Now if you just happen to have

18
00:45.470 --> 00:46.480
familiarity with

19
00:46.480 --> 00:50.850
Ansible the conditional logic would be invoked via the when keyword which again

20
00:50.850 --> 00:51.840
just like the loop

21
00:51.840 --> 00:56.100
keyword is ultimately acting as a front end a simplified front end for the

22
00:56.100 --> 00:57.520
underlying Python code

23
00:57.520 --> 01:01.660
with thin Ansible. Ginger to just happens to express this construct a lot more
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

23
00:57.520 --> 01:01.660
with thin Ansible. Ginger to just happens to express this construct a lot more

24
01:01.660 --> 01:03.120
like the native Python.

25
01:03.120 --> 01:06.020
Now we're going to notice quite a lot of the same things that we saw with

26
01:06.020 --> 01:07.200
respect to our loops once

27
01:07.200 --> 01:11.060
again the control structure is going to have the similar syntax we're going to

28
01:11.060 --> 01:12.080
have our curly brace

29
01:12.080 --> 01:14.910
as well as our percentage sign. The difference here is that we're going to use

30
01:14.910 --> 01:15.840
a different keyword

31
01:15.840 --> 01:19.330
this time instead of the four keyword of course we're going to be using the F

32
01:19.330 --> 01:20.320
keyword and again

33
01:20.320 --> 01:24.320
the close off the control structure with percentage and curly brace. Now this

34
01:24.320 --> 01:25.120
part here might like a

35
01:25.120 --> 01:28.600
little bit strange it just says if users and let me just not write over that

36
01:28.600 --> 01:29.920
let's try that again.

37
01:29.920 --> 01:33.640
In the case here what we're actually asking is a billion value that this means
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

37
01:29.920 --> 01:33.640
In the case here what we're actually asking is a billion value that this means

38
01:33.640 --> 01:34.960
if users is defined

39
01:34.960 --> 01:40.870
basically if we have users i.e. a users key within our variables that is going

40
01:40.870 --> 01:42.560
to be denoted as true

41
01:42.560 --> 01:46.310
and just like we talked about conditionals in a previous skill that means that

42
01:46.310 --> 01:47.040
the underlying

43
01:47.040 --> 01:50.750
code is going to execute. Now once again much like with respect to our loops we

44
01:50.750 --> 01:51.600
have to manually

45
01:51.600 --> 01:55.650
close them off with enginger to and the same thing is true with respect to our

46
01:55.650 --> 01:56.000
if statements

47
01:56.000 --> 02:00.070
we're going to have to use the end F keywords to effectively terminate any of
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

47
01:56.000 --> 02:00.070
we're going to have to use the end F keywords to effectively terminate any of

48
02:00.070 --> 02:00.960
these F blocks.

49
02:00.960 --> 02:05.320
Now look at this right here this example it says if users so pretty much if we

50
02:05.320 --> 02:07.440
have a users key defined

51
02:07.440 --> 02:11.180
then we want to do an action. Now what is the action? Well the action is to

52
02:11.180 --> 02:12.800
loop through all

53
02:12.800 --> 02:17.100
of the users we see four user end users and that gives us access to the iter

54
02:17.100 --> 02:17.840
able element and then

55
02:17.840 --> 02:23.540
we can parse out the username then notice we close the four loop here which is

56
02:23.540 --> 02:24.880
this part here and the

57
02:24.880 --> 02:29.500
outer if statement which is actually containing the four loop is closed at the

58
02:29.500 --> 02:30.480
very end hopefully
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

58
02:29.500 --> 02:30.480
very end hopefully

59
02:30.480 --> 02:36.420
that makes sense basically this part here only actually happens inside of the f

60
02:36.420 --> 02:37.600
condition so we

61
02:37.600 --> 02:41.270
essentially wrap the f condition around it here is where we define the start

62
02:41.270 --> 02:42.240
and here is where we

63
02:42.240 --> 02:46.620
define the end. Now we can actually create more complex if statements you can

64
02:46.620 --> 02:47.520
use things like

65
02:47.520 --> 02:52.110
LF to specify other conditions if the first condition does not pass as well as

66
02:52.110 --> 02:52.880
a default else

67
02:52.880 --> 02:56.700
which will always execute for now we're not going to worry about that let's

68
02:56.700 --> 02:57.840
just dive in and look

69
02:57.840 --> 03:02.730
at a basic if statement in conjunction with our current ginger to template. So
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

69
02:57.840 --> 03:02.730
at a basic if statement in conjunction with our current ginger to template. So

70
03:02.730 --> 03:03.520
if we go into our

71
03:03.520 --> 03:08.140
host variables for example let's perhaps remove the nugget user from the wheel

72
03:08.140 --> 03:08.960
group okay let's just

73
03:08.960 --> 03:12.840
edit this and what it will do is I'll remove the wheel group okay so DD to

74
03:12.840 --> 03:14.000
remove and let's do the

75
03:14.000 --> 03:18.870
same thing for the yellow box we're going here DD to remove and just saving qu

76
03:18.870 --> 03:20.240
its so if you recall

77
03:20.240 --> 03:24.790
in Linux the wheel group is used to denote admin users basically people who

78
03:24.790 --> 03:26.240
have elevated privileges

79
03:26.240 --> 03:29.660
perform the pseudo command that type of thing now the examples that I have

80
03:29.660 --> 03:30.800
written up here are not
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

80
03:29.660 --> 03:30.800
written up here are not

81
03:30.800 --> 03:34.010
necessarily accurate with respect to the real world configurations on these

82
03:34.010 --> 03:34.880
devices we're just

83
03:34.880 --> 03:39.320
trying to manipulate data here but as per this particular example we can see

84
03:39.320 --> 03:40.320
the answerable user

85
03:40.320 --> 03:44.540
as a member of wheel so they do have elevated privileges as well as James

86
03:44.540 --> 03:46.160
however the nugget user

87
03:46.160 --> 03:49.620
just appears to be a regular user right now because we deleted that wheel

88
03:49.620 --> 03:51.360
membership similarly if we

89
03:51.360 --> 03:56.480
look at the yellow box very same thing is true here answerable is once again an

90
03:56.480 --> 03:57.520
elevated user

91
03:57.520 --> 04:02.050
or a user with elevated privileges should I say as well as is the nox account
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

91
03:57.520 --> 04:02.050
or a user with elevated privileges should I say as well as is the nox account

92
04:02.050 --> 04:03.200
but the nugget user

93
04:03.200 --> 04:07.050
once again doesn't have that wheel access so let's imagine in our configuration

94
04:07.050 --> 04:07.920
fair we wanted to

95
04:07.920 --> 04:11.930
write which users happen to be admin users but we would like to have a

96
04:11.930 --> 04:13.680
particular condition if they

97
04:13.680 --> 04:18.470
happen to be a member of the wheel group we want to run the test and the test

98
04:18.470 --> 04:20.320
is if the wheel happens

99
04:20.320 --> 04:25.060
to appear then we can perform a particular action we can say something like the

100
04:25.060 --> 04:25.840
user has

101
04:25.840 --> 04:30.320
elevated privileges otherwise they do not have elevated privileges so that's
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

101
04:25.840 --> 04:30.320
elevated privileges otherwise they do not have elevated privileges so that's

102
04:30.320 --> 04:31.040
basically what we're

103
04:31.040 --> 04:35.290
going to try to do so let's go and check out our template right now go in and

104
04:35.290 --> 04:36.560
save them template

105
04:36.560 --> 04:41.320
test.j2 so what I could do is I could say as I'm looking through my users I can

106
04:41.320 --> 04:42.240
add my control

107
04:42.240 --> 04:46.810
logic so color brace percent and here is my condition if and then within my

108
04:46.810 --> 04:47.680
quotation marks

109
04:47.680 --> 04:53.050
I'll test if wheel is in the user dot groups and then I'll close off my control

110
04:53.050 --> 04:54.080
structure with

111
04:54.080 --> 04:59.010
percentage color brace then I can add in some new text perhaps I can say the

112
04:59.010 --> 05:00.960
users on my system
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

112
04:59.010 --> 05:00.960
users on my system

113
05:00.960 --> 05:06.160
with elevated priffs are and then we pass in the username and then we specify

114
05:06.160 --> 05:06.640
which groups

115
05:06.640 --> 05:10.420
they happen to be belonging to and the hard part here might be noticing where

116
05:10.420 --> 05:11.520
abouts to be closed

117
05:11.520 --> 05:16.930
off the if statement so let's work our way out to in here is the first first

118
05:16.930 --> 05:18.240
statement and that is

119
05:18.240 --> 05:22.160
closed off here at the end and then we have our inner first statement here

120
05:22.160 --> 05:23.840
which is closed off here

121
05:23.840 --> 05:28.640
now inside of the first statement here we have our if statement which means we

122
05:28.640 --> 05:29.280
actually want to

123
05:29.280 --> 05:33.160
have the if statement in between right here see it's kind of like a mirror
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

123
05:29.280 --> 05:33.160
have the if statement in between right here see it's kind of like a mirror

124
05:33.160 --> 05:34.320
reverse so what we want

125
05:34.320 --> 05:38.620
to do here is I'm going to go in between these two control structures and ender

126
05:38.620 --> 05:40.400
color brace percentage

127
05:40.400 --> 05:45.840
end if so if I press escape right and quit let's clear the screen and let's try

128
05:45.840 --> 05:46.720
rerunning that

129
05:46.720 --> 05:50.700
playbook and to enter and we can see the configurations that have been applied

130
05:50.700 --> 05:51.680
so let's go and check

131
05:51.680 --> 05:56.030
the config test file and check out the result it says there are users in fact

132
05:56.030 --> 05:56.960
that's a typo there

133
05:56.960 --> 06:01.140
that should say their users on my system with elevated priffs are and we can
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

133
05:56.960 --> 06:01.140
that should say their users on my system with elevated priffs are and we can

134
06:01.140 --> 06:02.160
see the answer will

135
06:02.160 --> 06:06.000
use it as well as the grip and in fact what we've done here notice we actually

136
06:06.000 --> 06:07.040
have a repetition

137
06:07.040 --> 06:11.050
of this statement the users on my system with elevated privileges that is

138
06:11.050 --> 06:12.080
because this statement

139
06:12.080 --> 06:16.040
here is actually also within the loop so it's being printed twice let's

140
06:16.040 --> 06:17.440
actually modify that and

141
06:17.440 --> 06:20.340
this is actually good to see because very often this is how you actually do

142
06:20.340 --> 06:21.440
these ginger templates

143
06:21.440 --> 06:26.480
you test it out before you actually deploy and you notice that there are some

144
06:26.480 --> 06:27.520
slight problems

145
06:27.520 --> 06:31.380
and what do you do just like a look you just iterate and iterate until it is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

145
06:27.520 --> 06:31.380
and what do you do just like a look you just iterate and iterate until it is

146
06:31.380 --> 06:33.200
correct so this part here

147
06:33.200 --> 06:38.080
as we can see is actually below therefore inside of a for loop so what I will

148
06:38.080 --> 06:39.200
do here is let's just

149
06:39.200 --> 06:43.680
remove this statement we don't need that and I'll just say the users on my

150
06:43.680 --> 06:44.960
system with elevated

151
06:44.960 --> 06:48.990
priffs are and that is now outside of the for loop unless you delete the one

152
06:48.990 --> 06:49.840
that is inside

153
06:49.840 --> 06:54.880
so if I press escape colon write and quits clear the screen add it up and rerun

154
06:54.880 --> 06:55.600
the playbook

155
06:55.600 --> 06:59.520
okay there's a changes in play let's go and verify so go into yellow box let's

156
06:59.520 --> 06:59.840
cat the

157
06:59.840 --> 07:04.040
config test this looks a lot better now it just says the users on my system
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

157
06:59.840 --> 07:04.040
config test this looks a lot better now it just says the users on my system

158
07:04.040 --> 07:05.120
with elevated priffs

159
07:05.120 --> 07:09.520
are and we see the answer will user and the groups to which answer belong to

160
07:09.520 --> 07:10.960
answer will and will

161
07:10.960 --> 07:14.580
similarly we also see Knox has elevated priffs and we see Knox's group

162
07:14.580 --> 07:15.280
membership

163
07:15.280 --> 07:19.560
Knox wheel and programming notice nowhere now because of that conditional

164
07:19.560 --> 07:20.240
statement

165
07:20.240 --> 07:24.240
do we actually see the nugget user and we should expect to see the same thing

166
07:24.240 --> 07:25.280
on the blue box

167
07:25.280 --> 07:29.750
so go to blue box and we'll cat the temp config test file enter check this out

168
07:29.750 --> 07:30.800
right here exactly
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

168
07:29.750 --> 07:30.800
right here exactly

169
07:30.800 --> 07:35.390
as we wanted to see the users on my system with elevated priffs are the answer

170
07:35.390 --> 07:36.000
will user with

171
07:36.000 --> 07:39.550
the corresponding groups as well as the James user with the corresponding

172
07:39.550 --> 07:40.480
groups once again

173
07:40.480 --> 07:43.720
and excluding that nugget user because of that conditional logic.
