WEBVTT

1
00:00.000 --> 00:03.440
So friends, throughout this skill we have discussed two different programming

2
00:03.440 --> 00:04.000
concepts.

3
00:04.000 --> 00:08.560
The first one was loops and the second one was conditional statements.

4
00:08.560 --> 00:12.920
Now we don't have to understand one in isolation or implement one in isolation

5
00:12.920 --> 00:13.760
of the other.

6
00:13.760 --> 00:16.960
We can in fact use loops with conditional statements.

7
00:16.960 --> 00:19.440
So in this video, that is what we're going to do.

8
00:19.440 --> 00:24.640
So let me create a new file once again, let's call this loop condition.YML.

9
00:24.640 --> 00:28.320
Again, I'm super creative as you know, it's going to insert mode, have our free

10
00:28.320 --> 00:28.560
high

11
00:28.560 --> 00:31.280
buttons at the top and let's give this playbook a name.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:28.560 --> 00:31.280
buttons at the top and let's give this playbook a name.

12
00:31.280 --> 00:36.080
Let's just say play to test loops with conditions.

13
00:36.080 --> 00:39.760
Perfect. So the host we are going to target is going to be the old group and

14
00:39.760 --> 00:40.400
all I will do

15
00:40.400 --> 00:46.000
is define my tasks. So let's have my task keyword followed by my array object

16
00:46.000 --> 00:46.160
and then

17
00:46.160 --> 00:49.910
we'll specify the name in this task. So the name here is going to be creating

18
00:49.910 --> 00:51.360
multiple files

19
00:51.360 --> 00:55.850
on UK devices. Now a game, we only have one device, but we can imagine the UK

20
00:55.850 --> 00:56.240
group is a

21
00:56.240 --> 00:59.600
lot larger than it actually is. Let's say we are not just managing two devices,

22
00:59.600 --> 01:00.160
but rather
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

22
00:59.600 --> 01:00.160
but rather

23
01:00.160 --> 01:03.840
500 devices in our network, makes it more fun that way after all.

24
01:03.840 --> 01:07.490
And the module we're going to use is going to be the Ansible built in shell

25
01:07.490 --> 01:08.160
module.

26
01:08.160 --> 01:11.360
So all I want to do here is to use the touch command.

27
01:11.360 --> 01:16.630
And what I want to do is I want to create a file called, let's maybe say UK one

28
01:16.630 --> 01:17.280
UK two

29
01:17.280 --> 01:20.640
and UK three if you happen to be in the UK group.

30
01:20.640 --> 01:24.400
So I don't want to have to type UK one dot txt here because that would hard

31
01:24.400 --> 01:25.200
code the value.

32
01:25.200 --> 01:28.450
That would mean we would have to continually retype more tasks defining the

33
01:28.450 --> 01:29.120
shell module

34
01:29.120 --> 01:33.110
so on so forth, definitely not efficient. So what I will do is I will say touch
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:29.120 --> 01:33.110
so on so forth, definitely not efficient. So what I will do is I will say touch

35
01:33.110 --> 01:33.680
and then I'll have

36
01:33.680 --> 01:37.000
my double color braces here. Now what am I going to actually have within the

37
01:37.000 --> 01:37.760
parentheses? Well,

38
01:37.760 --> 01:41.050
we're going to be looping through some particular details. So let's go out

39
01:41.050 --> 01:42.080
there with them.

40
01:42.080 --> 01:46.450
Use the loop keywords and we'll specify as an array all of the elements you

41
01:46.450 --> 01:47.200
want to loop through.

42
01:47.200 --> 01:53.360
So let's just say UK one dot txt. The next one is UK two dot txt and the third

43
01:53.360 --> 01:54.720
one can be UK three

44
01:54.720 --> 01:58.890
dot txt. So if you want to access each element within the loop, how do we do

45
01:58.890 --> 01:59.840
that? Well, we use

46
01:59.840 --> 02:03.410
the item keyboard as we saw before. Okay, let's create a new task then we'll
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

46
01:59.840 --> 02:03.410
the item keyboard as we saw before. Okay, let's create a new task then we'll

47
02:03.410 --> 02:04.240
create a name.

48
02:04.240 --> 02:10.300
I must specify the name as creates multiple files on USA devices. So it really

49
02:10.300 --> 02:11.200
similar to before,

50
02:11.200 --> 02:14.860
we'll use the Ansible built in shell module and what we're going to do similar

51
02:14.860 --> 02:15.840
to as we saw before,

52
02:15.840 --> 02:19.680
we're going to use the touch command to create brand new files. But once again,

53
02:19.680 --> 02:23.760
we're going to be using a variable and we're going to be leveraging the power

54
02:23.760 --> 02:24.320
of lips. So we'll

55
02:24.320 --> 02:28.880
use the keyboard item use a lip keyword again, specifying array this time,

56
02:28.880 --> 02:29.520
though, the values

57
02:29.520 --> 02:35.280
are going to be USA one dot txt, USA two dot txt. And let's maybe add another
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:29.520 --> 02:35.280
are going to be USA one dot txt, USA two dot txt. And let's maybe add another

58
02:35.280 --> 02:36.000
one just called

59
02:36.000 --> 02:39.840
America dot txt doesn't actually matter. Now, of course, what's going to happen

60
02:39.840 --> 02:40.400
is right now,

61
02:40.400 --> 02:44.870
this is going to run for all devices, we do not want that. We want to add a

62
02:44.870 --> 02:46.480
conditional with the

63
02:46.480 --> 02:50.490
loop element. So at the same level of indentation as the loop, what I'm going

64
02:50.490 --> 02:51.680
to do is I'm going to

65
02:51.680 --> 02:56.260
invoke the when keyword here. So I'll just say when and have my colon to make a

66
02:56.260 --> 02:57.600
key. And as we saw

67
02:57.600 --> 03:01.490
before, I'm going to have my double quotation marks. And inside of that, I will
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:57.600 --> 03:01.490
before, I'm going to have my double quotation marks. And inside of that, I will

68
03:01.490 --> 03:02.160
have my single

69
03:02.160 --> 03:06.420
quotation marks to specify what it is I'm targeting. I'm going to specify the

70
03:06.420 --> 03:07.600
UK group once again

71
03:07.600 --> 03:11.630
in lowercase. Let's not make that same mistake again. And then I will say in

72
03:11.630 --> 03:12.640
group underscore

73
03:12.640 --> 03:16.230
names calling that magic variable once again. And the same thing is going to be

74
03:16.230 --> 03:17.120
true with a slight

75
03:17.120 --> 03:20.750
modification for the next task, we'll use the when keyword, have a colon,

76
03:20.750 --> 03:22.320
double quotation marks,

77
03:22.320 --> 03:27.320
and our single quotation marks within this time will specify USA in the magic

78
03:27.320 --> 03:28.400
variable group

79
03:28.400 --> 03:32.360
underscore names. So with that now done, what I can do is press escape, writing
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

79
03:28.400 --> 03:32.360
underscore names. So with that now done, what I can do is press escape, writing

80
03:32.360 --> 03:33.440
quit to save the

81
03:33.440 --> 03:37.660
file. Let's hit enter. And if I just say answer will have in playbook loop

82
03:37.660 --> 03:38.960
condition dot yml

83
03:38.960 --> 03:42.770
and to enter answer, I'm like security. And we can see the outputs are let's

84
03:42.770 --> 03:44.080
scroll up a little bit.

85
03:44.080 --> 03:48.850
So as we can see here in the output for task one, create multiple files on the

86
03:48.850 --> 03:49.520
UK device,

87
03:49.520 --> 03:55.070
we can see here for the yellow box, which is a USA device. This particular task

88
03:55.070 --> 03:56.080
has been skipped.

89
03:56.080 --> 04:01.200
You see that however, when it comes to the blue box, it does actually specify
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

89
03:56.080 --> 04:01.200
You see that however, when it comes to the blue box, it does actually specify

90
04:01.200 --> 04:02.240
that this has changed

91
04:02.240 --> 04:08.510
and it's changed for the item UK one dot txt UK two dot txt as well as UK three

92
04:08.510 --> 04:09.200
dot txt.

93
04:09.200 --> 04:13.220
Once again, though, at the next task, the inverse is true. When you want to

94
04:13.220 --> 04:14.240
create multiple files

95
04:14.240 --> 04:19.740
on USA devices, the blue box has been skipped for all three items USA one USA

96
04:19.740 --> 04:20.800
two and America

97
04:20.800 --> 04:25.010
dot txt, whereas for the yellow box, which does belong to the USA group for

98
04:25.010 --> 04:26.560
this particular task,

99
04:26.560 --> 04:31.210
we have actually changed the device again for the item USA one USA two and
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

99
04:26.560 --> 04:31.210
we have actually changed the device again for the item USA one USA two and

100
04:31.210 --> 04:32.720
America dot txt.

101
04:32.720 --> 04:37.250
So let's go to our respective devices starting with the yellow box. Now, be

102
04:37.250 --> 04:38.000
aware, because I

103
04:38.000 --> 04:41.840
didn't specify a full absolute path file has been written into the home

104
04:41.840 --> 04:43.120
directory of the user.

105
04:43.120 --> 04:47.930
Now right now, I am the nugget user, as we can see here, really whom I command.

106
04:47.930 --> 04:48.400
However,

107
04:48.400 --> 04:52.720
because Ansible is executing as the user Ansible, it's going to be in that

108
04:52.720 --> 04:53.600
particular directory.

109
04:53.600 --> 04:57.480
Now, if I go into the home directory here, if I try to access Ansible, notice

110
04:57.480 --> 04:58.000
the permission is

111
04:58.000 --> 05:02.180
denied. So right now, all I will say is Sue Ansible for substitute user. If I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

111
04:58.000 --> 05:02.180
denied. So right now, all I will say is Sue Ansible for substitute user. If I

112
05:02.180 --> 05:03.200
insert, I'll type in

113
05:03.200 --> 05:06.790
the answerable password, which is just the word Ansible. Now I am the answer

114
05:06.790 --> 05:07.680
able user. If I do

115
05:07.680 --> 05:11.300
an LS, we should be able to go into the answerable directory. And if I do an LS

116
05:11.300 --> 05:12.320
here, as we can see

117
05:12.320 --> 05:16.420
here, we do have these particular files, let's do the same thing on the blue

118
05:16.420 --> 05:17.440
box. So once again,

119
05:17.440 --> 05:21.150
on the blue box, I am the nugget user, let's substitute user to Ansible, type

120
05:21.150 --> 05:22.000
in the password

121
05:22.000 --> 05:27.170
of Ansible. If I now say who am I, I am Ansible. If I go to cd home and enter

122
05:27.170 --> 05:28.400
the Ansible directory

123
05:28.400 --> 05:33.860
to an LS, as we can see here, we actually have the UK files UK one UK two and
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

123
05:28.400 --> 05:33.860
to an LS, as we can see here, we actually have the UK files UK one UK two and

124
05:33.860 --> 05:35.280
UK three. So now that

125
05:35.280 --> 05:39.360
we've seen how to use loops as well as conditional statements, as well as loops

126
05:39.360 --> 05:40.320
with conditional

127
05:40.320 --> 05:43.680
statements, let's now validate what we've learned within this scale.
