WEBVTT

1
00:00.000 --> 00:04.160
So friends, in this particular video, what I want to talk to you about is one

2
00:04.160 --> 00:05.040
of the most

3
00:05.040 --> 00:09.460
important concepts in all of programming. What exactly am I talking about? Well

4
00:09.460 --> 00:10.480
, let's just find

5
00:10.480 --> 00:15.070
out. So drum roll please, what exactly am I talking about? Well, we're talking

6
00:15.070 --> 00:17.360
about the idea of loops.

7
00:17.360 --> 00:21.120
Now, I don't want you to get scared thinking you have to really understand

8
00:21.120 --> 00:22.560
programming at a deep

9
00:22.560 --> 00:26.460
level. But here's the thing, ultimately under the hood, Ansible is actually

10
00:26.460 --> 00:28.320
written to use the Python

11
00:28.320 --> 00:32.600
programming language. So basically, Ansible acts as an abstraction layer
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:28.320 --> 00:32.600
programming language. So basically, Ansible acts as an abstraction layer

12
00:32.600 --> 00:34.000
whereby you specify your

13
00:34.000 --> 00:38.470
programmatic logic in YAML, which is a lot more human readable. And Ansible is

14
00:38.470 --> 00:39.600
going to translate

15
00:39.600 --> 00:44.520
that into the underlying Python to execute the actual task. So the net effect

16
00:44.520 --> 00:45.520
of this is that

17
00:45.520 --> 00:50.500
the concepts within Python, i.e. these programmatic concepts are actually

18
00:50.500 --> 00:52.480
applicable when using Ansible,

19
00:52.480 --> 00:57.390
we just simply utilize them in a slightly different and more simplified manner.

20
00:57.390 --> 00:58.080
So we're not going to

21
00:58.080 --> 01:02.280
have to write Python code, but it is a good idea to understand the underlying
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

21
00:58.080 --> 01:02.280
have to write Python code, but it is a good idea to understand the underlying

22
01:02.280 --> 01:03.440
components of this

23
01:03.440 --> 01:07.350
programmatic logic. So let's talk about loops and find out exactly what they

24
01:07.350 --> 01:08.640
are. So like I say,

25
01:08.640 --> 01:12.720
loops are a core component of programming. And this is something that is very,

26
01:12.720 --> 01:13.520
very powerful,

27
01:13.520 --> 01:18.450
and is applicable in pretty much all programming languages. The whole purpose

28
01:18.450 --> 01:19.440
of this is as the

29
01:19.440 --> 01:23.500
name does suggest, it's going to allow you to perform particular tasks in a

30
01:23.500 --> 01:24.880
repetitive fashion.

31
01:24.880 --> 01:29.190
Now that doesn't actually mean do everything the exact same all the time. We

32
01:29.190 --> 01:30.000
can actually make
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

33
01:30.000 --> 01:34.480
slight modifications as we perform these repetitive tasks, but it's the loop

34
01:34.480 --> 01:35.440
construct that is going

35
01:35.440 --> 01:39.240
to allow us to perform these actions efficiently. So let's maybe say I wanted

36
01:39.240 --> 01:40.160
to greet a whole

37
01:40.160 --> 01:45.040
room of people. So what I would like to say is hello, then the person's name,

38
01:45.040 --> 01:45.840
and then ask them

39
01:45.840 --> 01:50.360
how are you? Well, suppose I was giving the same greeting for every single

40
01:50.360 --> 01:51.920
person, this part here,

41
01:51.920 --> 01:56.480
as well as this part here would be the exact same, we would be repeating them.

42
01:56.480 --> 01:57.120
The power though,

43
01:57.120 --> 02:01.130
is in respect to this part here, which would actually be represented as a
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

43
01:57.120 --> 02:01.130
is in respect to this part here, which would actually be represented as a

44
02:01.130 --> 02:02.080
variable value.

45
02:02.080 --> 02:05.990
We talked about variables any previous skill. So suppose we happen to in our y

46
02:05.990 --> 02:06.560
aml file,

47
02:06.560 --> 02:10.960
happen to have a people key, and it was a list object. And we had a whole bunch

48
02:10.960 --> 02:11.280
of people

49
02:11.280 --> 02:16.840
to find such as Simone and James, as well as let's maybe say Knox and we'll say

50
02:16.840 --> 02:17.440
Keith.

51
02:17.440 --> 02:22.320
So programmatically, what I could do is I could invoke a loop to loop through

52
02:22.320 --> 02:23.440
all of the entries

53
02:23.440 --> 02:28.210
within my people list, that is Simone, James, Knox and Keith, and substitute

54
02:28.210 --> 02:29.280
those values into

55
02:29.280 --> 02:33.100
this variable session here. And the net effect would actually be we could
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

55
02:29.280 --> 02:33.100
this variable session here. And the net effect would actually be we could

56
02:33.100 --> 02:34.240
programmatically render

57
02:34.240 --> 02:38.720
the value hello, Simone, how are you? And then once that has been performed, we

58
02:38.720 --> 02:39.200
would go through

59
02:39.200 --> 02:42.870
all of the other entries such as hello, Knox, how are you, Hello Keith, how are

60
02:42.870 --> 02:44.080
you that type of thing.

61
02:44.080 --> 02:47.200
Pretty much what we're talking about here is the use of something known as a

62
02:47.200 --> 02:49.040
for loop within Python.

63
02:49.040 --> 02:53.210
Basically, we're going to have the program iterate over a predefined sequence

64
02:53.210 --> 02:54.000
of values,

65
02:54.000 --> 02:58.280
such as in the example I used four different names. Now, whilst we happen to be

66
02:58.280 --> 02:58.960
using what is

67
02:58.960 --> 03:03.840
known as a for loop using the Python terminology, we don't actually use the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:58.960 --> 03:03.840
known as a for loop using the Python terminology, we don't actually use the

68
03:03.840 --> 03:04.880
same type of syntax

69
03:04.880 --> 03:09.440
that you would find in a piece of Python code. Instead, Ansible has its own way

70
03:09.440 --> 03:10.080
to implement

71
03:10.080 --> 03:14.250
this type of logic, like I say, Ansible is all a bit abstraction and simpl

72
03:14.250 --> 03:15.120
ification.

73
03:15.120 --> 03:19.680
So one of the best things to do is to go to the Ansible documentation to learn

74
03:19.680 --> 03:20.080
how we can

75
03:20.080 --> 03:23.980
actually implement these loops. So with that said, let's go right there right

76
03:23.980 --> 03:25.120
now. So all I've done

77
03:25.120 --> 03:28.730
is went to Google and search for loops answer. What I will do here is I will

78
03:28.730 --> 03:29.840
click this top line

79
03:29.840 --> 03:34.810
right here. So here's what it says, it says Ansible offers the loop, as well as
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

79
03:29.840 --> 03:34.810
right here. So here's what it says, it says Ansible offers the loop, as well as

80
03:34.810 --> 03:36.560
the width_lookup

81
03:36.560 --> 03:40.980
and untill keywords to execute tasks multiple times. This is what we're talking

82
03:40.980 --> 03:41.840
about. Okay,

83
03:41.840 --> 03:46.290
executing tasks multiple times, we are looping. Now, one thing to note is the

84
03:46.290 --> 03:47.520
in modern iterations

85
03:47.520 --> 03:52.480
of Ansible, the look keyword is the preferred way to do this. The width lookup

86
03:52.480 --> 03:53.680
is the deprecated

87
03:53.680 --> 03:57.920
version. So let's look at some examples here. So if we go to standard loops

88
03:57.920 --> 03:59.040
right now, take this

89
03:59.040 --> 04:04.080
example here. Okay. So we have a little task here, the name is to add several
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

89
03:59.040 --> 04:04.080
example here. Okay. So we have a little task here, the name is to add several

90
04:04.080 --> 04:05.280
users. And all we're

91
04:05.280 --> 04:09.680
doing is using the Ansible built in user module. We saw this module earlier on

92
04:09.680 --> 04:10.720
within a previous

93
04:10.720 --> 04:14.900
skill. Now ignore this part for the moments. But what we're seeing here is that

94
04:14.900 --> 04:15.920
we're going to add

95
04:15.920 --> 04:19.280
these particular users to the wheel group, which is basically the super user

96
04:19.280 --> 04:20.160
group. And we want

97
04:20.160 --> 04:24.720
them to be present. What do we actually want to loop through? Well, as we just

98
04:24.720 --> 04:25.680
saw, we're going to

99
04:25.680 --> 04:29.870
use the loop keywords. Now, because we're going to be looping through multiple

100
04:29.870 --> 04:30.960
items, we know when
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

100
04:29.870 --> 04:30.960
items, we know when

101
04:30.960 --> 04:35.240
we specify via YAML, we can use this hyphenation to denote an array object. So

102
04:35.240 --> 04:35.920
pretty much, we're

103
04:35.920 --> 04:39.980
going to be looping through test user one, and then test user two to repetitive

104
04:39.980 --> 04:40.880
ly create these

105
04:40.880 --> 04:45.180
new users on the system. Now this part here is important. So we can see our

106
04:45.180 --> 04:46.480
double curly braces

107
04:46.480 --> 04:51.890
here. And inside of this, we have a very particular keyword known as item. This

108
04:51.890 --> 04:52.880
is actually a special

109
04:52.880 --> 04:57.910
piece of syntax within Ansible that is going to represent each item of the loop

110
04:57.910 --> 04:59.040
. So effectively,

111
04:59.040 --> 05:03.560
all we're seeing here is that when it comes to creating this new user, upon
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

111
04:59.040 --> 05:03.560
all we're seeing here is that when it comes to creating this new user, upon

112
05:03.560 --> 05:04.640
first iteration,

113
05:04.640 --> 05:08.400
we're going to take this value within the actual loop. And we're just going to

114
05:08.400 --> 05:10.080
grab it here and

115
05:10.080 --> 05:14.870
stick it in right here as a variable. So the name is going to be test user one

116
05:14.870 --> 05:16.240
with the state present

117
05:16.240 --> 05:20.570
and inside of the wheel group. Upon next iteration, once we have done this

118
05:20.570 --> 05:22.080
particular task, we move

119
05:22.080 --> 05:26.720
on to the next one. And then we just take the test user to and then populate it

120
05:26.720 --> 05:27.760
right in here,

121
05:27.760 --> 05:32.510
where the variable is. And once again, we say name, test user to the state is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

121
05:27.760 --> 05:32.510
where the variable is. And once again, we say name, test user to the state is

122
05:32.510 --> 05:34.080
present. And the group

123
05:34.080 --> 05:38.300
that the test user to is going to belong to is also going to be the wheel group

124
05:38.300 --> 05:39.280
. This is basically

125
05:39.280 --> 05:43.290
what we're talking about. So let's just see this item keyword and action then.

126
05:43.290 --> 05:43.920
So let's create a

127
05:43.920 --> 05:48.230
file just call let's maybe say lipper dot yml, and we'll go in and edit it. So

128
05:48.230 --> 05:49.360
at the very top,

129
05:49.360 --> 05:53.770
have a free hyphen to denote a yaml file. So the name of the plate is going to

130
05:53.770 --> 05:54.960
be look example.

131
05:54.960 --> 05:58.480
I'll say the host will be local host. That means it's just going to execute on

132
05:58.480 --> 05:59.520
my local system

133
05:59.520 --> 06:04.160
right here as opposed to the manage devices. And I'm going to specify my tasks.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

133
05:59.520 --> 06:04.160
right here as opposed to the manage devices. And I'm going to specify my tasks.

134
06:04.160 --> 06:04.720
So this one will

135
06:04.720 --> 06:09.450
just be a loop through some server IPs. The right now I'll just define some

136
06:09.450 --> 06:10.880
variables directly within

137
06:10.880 --> 06:14.260
the playbook. Again, we saw how to do this any previous skill. And I'll just

138
06:14.260 --> 06:15.120
call the servers

139
06:15.120 --> 06:18.520
my servers and it's going to be an array object. So we'll have the IP address

140
06:18.520 --> 06:19.440
of one dot one dot one

141
06:19.440 --> 06:25.630
and then two dot two dot two as well as three four and five. Now right now just

142
06:25.630 --> 06:26.480
for simplicity,

143
06:26.480 --> 06:29.380
I'm just going to use the debug module to print out a message. This is

144
06:29.380 --> 06:30.720
obviously not any type of
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

144
06:29.380 --> 06:30.720
obviously not any type of

145
06:30.720 --> 06:34.490
configurations I'm making, but you can imagine I could use the shell module to

146
06:34.490 --> 06:35.280
actually have

147
06:35.280 --> 06:39.040
this information be populated into a file. But like I say for now, let's just

148
06:39.040 --> 06:39.760
keep this very,

149
06:39.760 --> 06:43.520
very simple. So let's just say something like I have configured and I'll just

150
06:43.520 --> 06:44.400
hard code this right

151
06:44.400 --> 06:49.460
now one dot one dot one dot one as a DNS server. Okay, so let's briefly save

152
06:49.460 --> 06:50.960
this right and quits.

153
06:50.960 --> 06:55.850
If I just say Ansible, hive and playbook on lipper and to enter. And we can see

154
06:55.850 --> 06:56.800
here the result of

155
06:56.800 --> 07:00.500
our task with printed out this message, I have configured a one dot one dot one
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

155
06:56.800 --> 07:00.500
our task with printed out this message, I have configured a one dot one dot one

156
07:00.500 --> 07:01.680
dot one as a DNS

157
07:01.680 --> 07:05.900
server. So let's say we wanted to make use of all of those servers so that we

158
07:05.900 --> 07:07.120
could just continually

159
07:07.120 --> 07:12.180
print out this particular message here I have configured. And then the actual

160
07:12.180 --> 07:13.600
value as a DNS

161
07:13.600 --> 07:17.010
server much in the same way we talked about the greeting hey, and then insert

162
07:17.010 --> 07:17.840
the person's name

163
07:17.840 --> 07:21.780
how are you? So let's make this modification go back into lipper. So this

164
07:21.780 --> 07:22.960
portion here the hard

165
07:22.960 --> 07:26.590
code of version what I want to do is to delete this and I want to populate it

166
07:26.590 --> 07:27.520
with a variable

167
07:27.520 --> 07:32.170
value. Now this particular variable value is going to be populated with each
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

167
07:27.520 --> 07:32.170
value. Now this particular variable value is going to be populated with each

168
07:32.170 --> 07:33.520
item within error rate

169
07:33.520 --> 07:38.280
as denoted via the my servers variable. So what I'm going to do here is at the

170
07:38.280 --> 07:40.080
same level of indentation

171
07:40.080 --> 07:45.230
as the debug module, I'm going to use my loop keywords. And now I specify what

172
07:45.230 --> 07:45.840
is it I want to

173
07:45.840 --> 07:49.140
loop through? Well, I want to loop through my servers. And how am I going to

174
07:49.140 --> 07:50.320
represent each item

175
07:50.320 --> 07:53.760
within the loop as this variable right here? Well, as we saw from a

176
07:53.760 --> 07:55.680
documentation, we use a very

177
07:55.680 --> 07:59.480
particular keyword. And that is the item keywords, which is going to pass in

178
07:59.480 --> 08:00.560
every single thing we
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

178
07:59.480 --> 08:00.560
every single thing we

179
08:00.560 --> 08:04.000
loop through. So let's actually try to write this out and save this rerun the

180
08:04.000 --> 08:05.440
playbook hit enter.

181
08:05.440 --> 08:08.580
Oh, and it appears we have an error here. This is actually very, very important

182
08:08.580 --> 08:09.840
. Let me go back in

183
08:09.840 --> 08:13.280
what I want to ensure here is that my servers here, I'll put them with

184
08:13.280 --> 08:16.900
implementation marks. And I also want to use my double curly braces for the

185
08:16.900 --> 08:17.920
item I'm looking through.

186
08:17.920 --> 08:22.400
So let's try this one more time rerun the playbook and check out the result as

187
08:22.400 --> 08:23.520
we can see here,

188
08:23.520 --> 08:29.890
we have looked through all of the individual items within array 1.1.1.1 2.2.2.2

189
08:29.890 --> 08:30.320
.2.1. So on so
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

189
08:29.890 --> 08:30.320
.2.1. So on so

190
08:30.320 --> 08:34.550
forth. And we have populated that into a what would be a repetitive message. I

191
08:34.550 --> 08:35.440
have configured

192
08:35.440 --> 08:40.540
1.1.1.1 as a DNS server. And then we move on to the next, I have configured 2.2

193
08:40.540 --> 08:41.600
.2.2 as a DNS

194
08:41.600 --> 08:46.420
server all the way until we exhaust the list and in the end here specifying I

195
08:46.420 --> 08:48.640
have configured 5.5.5.5

196
08:48.640 --> 08:51.040
as a DNS server.
