WEBVTT

1
00:00.000 --> 00:04.510
So friends, previously we learned how we could create our host variables as

2
00:04.510 --> 00:06.000
well as group variables.

3
00:06.000 --> 00:10.480
Now the actual data we populated within these files was really quite simple.

4
00:10.480 --> 00:15.760
What if the data was a lot more complex, i.e. what if it was nested data?

5
00:15.760 --> 00:19.280
How do we actually handle that? Well, let's find out and get to it.

6
00:19.280 --> 00:22.720
So let's say for example going to the host first directory and what I will do

7
00:22.720 --> 00:23.280
is I will go into

8
00:23.280 --> 00:27.360
blue box. Inside of here I'm going to add in more data. So let's maybe go with

9
00:27.360 --> 00:28.000
a key called,

10
00:28.000 --> 00:31.120
or I don't know, let's just say Linux, okay. And then we'll specify the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

10
00:28.000 --> 00:31.120
or I don't know, let's just say Linux, okay. And then we'll specify the

11
00:31.120 --> 00:32.000
operating system

12
00:32.000 --> 00:36.720
and we just say red hat enterprise Linux. Then we'll specify the version which

13
00:36.720 --> 00:37.520
in my case here

14
00:37.520 --> 00:41.520
is going to be version 9.2 and let's just create another key called random

15
00:41.520 --> 00:42.160
because I'm running out

16
00:42.160 --> 00:47.560
of ideas. Let's just say domain name is www.bluebox.com whatever, doesn't

17
00:47.560 --> 00:49.280
actually matter and we'll just

18
00:49.280 --> 00:54.210
say the nickname is simply blue box. Okay, so a little bit of nested data we

19
00:54.210 --> 00:55.280
saw when we wanted

20
00:55.280 --> 00:59.810
to retrieve information relating to the test file right here. All we did was

21
00:59.810 --> 01:01.360
create parentheses and
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

21
00:59.810 --> 01:01.360
create parentheses and

22
01:01.360 --> 01:06.740
pop this particular value in. Now, what if we wanted to access maybe say this

23
01:06.740 --> 01:07.600
value here.

24
01:07.600 --> 01:11.910
Let's try this out. Let's just create a little playbook here called nested.YML

25
01:11.910 --> 01:12.240
create three

26
01:12.240 --> 01:16.790
hyphens, give it a name of parse nested data. The host I'm going to target in

27
01:16.790 --> 01:17.760
this case here is

28
01:17.760 --> 01:21.930
just simply going to be the blue box and the tasks. Now the task here I'm going

29
01:21.930 --> 01:22.960
to give a name and

30
01:22.960 --> 01:27.050
the name is going to be print out some var and for care. Now the module I'm

31
01:27.050 --> 01:28.000
going to use here is

32
01:28.000 --> 01:31.570
going to be the debug module. This is a module we can use to basically print
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

32
01:28.000 --> 01:31.570
going to be the debug module. This is a module we can use to basically print

33
01:31.570 --> 01:32.800
out messages. So I'll

34
01:32.800 --> 01:36.060
just say the message I want to print out here is going to parse out this

35
01:36.060 --> 01:37.360
particular detail here,

36
01:37.360 --> 01:42.300
the version key. Now notice if I just call the key right here, just say I am

37
01:42.300 --> 01:44.240
running rail version

38
01:44.240 --> 01:47.590
and then I do my double curly braces and I call the version key. Watch what

39
01:47.590 --> 01:48.400
happens when I run

40
01:48.400 --> 01:53.040
this. So I'll say write and quit. So Ansible playbook nested. Look at this

41
01:53.040 --> 01:53.840
output right here.

42
01:53.840 --> 01:58.450
We have a fatal result and it says here this task includes an option with an

43
01:58.450 --> 02:00.400
undefined variable.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

43
01:58.450 --> 02:00.400
undefined variable.

44
02:00.400 --> 02:04.480
The error was version is undefined. So we can't just go into the variable file

45
02:04.480 --> 02:05.520
and just call the key

46
02:05.520 --> 02:10.500
here. The reason being is because this key is actually nested within another

47
02:10.500 --> 02:11.760
key. Notice we have

48
02:11.760 --> 02:16.160
this Linux key first, which is actually the parent to version. So pretty much

49
02:16.160 --> 02:17.200
what I have to do is

50
02:17.200 --> 02:22.480
I have to walk the path starting from the outermost level into the information

51
02:22.480 --> 02:23.680
that I actually want.

52
02:23.680 --> 02:28.830
So notice that the outermost level here is indeed a Linux. Okay. So that is the

53
02:28.830 --> 02:29.360
key we're going to

54
02:29.360 --> 02:34.400
call first. So if I just remove version and replace it with Linux and I just
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

54
02:29.360 --> 02:34.400
call first. So if I just remove version and replace it with Linux and I just

55
02:34.400 --> 02:35.920
write and save and to

56
02:35.920 --> 02:39.780
enter, let's read run the playbook. Okay. So we didn't actually get an error.

57
02:39.780 --> 02:40.640
But if you notice

58
02:40.640 --> 02:44.450
what we actually are printing out, it isn't the information that we really want

59
02:44.450 --> 02:45.200
. It says you

60
02:45.200 --> 02:49.440
the message is I am running rail version. And what we actually have here is

61
02:49.440 --> 02:50.320
basically the structured

62
02:50.320 --> 02:54.140
data that is nested within that particular key. You see that we have our square

63
02:54.140 --> 02:55.200
brackets, then we

64
02:55.200 --> 02:58.820
have operating system and then the corresponding value right at enterprise

65
02:58.820 --> 02:59.840
Linux. Then we have

66
02:59.840 --> 03:04.670
the version key followed up with the actual value 9.2. This here is the value
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

66
02:59.840 --> 03:04.670
the version key followed up with the actual value 9.2. This here is the value

67
03:04.670 --> 03:06.080
we want to get. So

68
03:06.080 --> 03:10.690
pretty much we want to use something known as dot notation to actually access

69
03:10.690 --> 03:11.680
this data program

70
03:11.680 --> 03:16.720
automatically. So realistically, we're going to specify the Linux key within

71
03:16.720 --> 03:18.000
our curly braces.

72
03:18.000 --> 03:22.530
But to get the information within the version key, I'm going to say dot and

73
03:22.530 --> 03:23.760
then give the next key,

74
03:23.760 --> 03:28.720
which in this case here is version that should actually just return this data

75
03:28.720 --> 03:30.000
here as opposed to
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

76
03:30.000 --> 03:34.890
returning all of the data contained within the Linux key. So let's check it out

77
03:34.890 --> 03:35.760
. Go back in,

78
03:35.760 --> 03:39.280
make a slight adjustment, go into insert mode and we'll just change this to

79
03:39.280 --> 03:40.560
Linux dot version.

80
03:40.560 --> 03:44.590
If I write in quits and I read on the playbook, boom, check it out. I am

81
03:44.590 --> 03:46.080
running rail version

82
03:46.080 --> 03:50.320
and then we actually parsed out the version information 9.2. Similarly, if I

83
03:50.320 --> 03:50.960
wanted to parse

84
03:50.960 --> 03:54.600
out something different, something even more nested, such as, oh, I don't know,

85
03:54.600 --> 03:55.840
maybe the domain name

86
03:55.840 --> 03:59.820
here, what I'm going to do is to parse out the Linux key, which gives us access

87
03:59.820 --> 04:01.040
to all this data.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

87
03:59.820 --> 04:01.040
to all this data.

88
04:01.040 --> 04:05.850
Then I can parse out the random key, which gives us access to all of this data

89
04:05.850 --> 04:06.560
and then parse out

90
04:06.560 --> 04:11.390
the domain name key, which gives us precisely this piece of data. So if I just

91
04:11.390 --> 04:12.480
go into the playbook,

92
04:12.480 --> 04:16.110
once again, sort of parse out the rail version, while also adding the domain

93
04:16.110 --> 04:17.040
name information,

94
04:17.040 --> 04:21.120
which say my domain is double curly braces once again, and we'll just call

95
04:21.120 --> 04:23.200
Linux random domain

96
04:23.200 --> 04:27.660
underscore name. Okay, so let's go and try this now. We run the playbook hit

97
04:27.660 --> 04:28.560
enter. Look at this

98
04:28.560 --> 04:34.920
right here. The resultant message is a rail version 9.2. My domain is www dot
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

98
04:28.560 --> 04:34.920
right here. The resultant message is a rail version 9.2. My domain is www dot

99
04:34.920 --> 04:36.000
blue box dot com.

100
04:36.000 --> 04:40.400
Everything has been parsed correctly, even parsing things at different levels

101
04:40.400 --> 04:42.000
of indentation.

102
04:42.000 --> 04:45.180
Now, let me just briefly make a modification one more time. So I'll go into

103
04:45.180 --> 04:46.400
blue box once again,

104
04:46.400 --> 04:51.600
and let's maybe add on a users key. Okay. Now, because we have multiple users,

105
04:51.600 --> 04:55.260
I'm going to have an array here, which is going to be denoted via a hyphen to

106
04:55.260 --> 04:55.920
the first user is

107
04:55.920 --> 04:59.400
going to be nugget user. And the second user, well, that's going to be the

108
04:59.400 --> 05:00.480
answerable user account
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

108
04:59.400 --> 05:00.480
answerable user account

109
05:00.480 --> 05:03.580
we've already created. But for fun, let's just add in some more information,

110
05:03.580 --> 05:05.040
let's just say John

111
05:05.040 --> 05:09.720
and Simone. How are we actually going to access, say, for example, the John

112
05:09.720 --> 05:10.800
data right here,

113
05:10.800 --> 05:14.560
we know we should reference the Linux outer key and then inside of that, the

114
05:14.560 --> 05:15.360
user's key,

115
05:15.360 --> 05:18.970
but there isn't corresponding keys for each individual entry here, because we

116
05:18.970 --> 05:19.600
're dealing with

117
05:19.600 --> 05:24.830
an array. So let's actually see this in action. Now, I have a user called an N

118
05:24.830 --> 05:26.400
my double curly braces,

119
05:26.400 --> 05:31.030
and I'll say Linux users on my system. Now, let me just briefly save this for
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

119
05:26.400 --> 05:31.030
and I'll say Linux users on my system. Now, let me just briefly save this for

120
05:31.030 --> 05:32.240
now, run it again.

121
05:32.240 --> 05:36.480
So we can see here what we've parsed out is effectively an array object, says I

122
05:36.480 --> 05:37.040
have a user

123
05:37.040 --> 05:40.600
called and then we see the entire list of users nugget user and sable john sam

124
05:40.600 --> 05:41.920
ona on my system.

125
05:41.920 --> 05:45.380
But let's say we want to just get out john. Now, the crucial point here is that

126
05:45.380 --> 05:46.640
we do not access

127
05:46.640 --> 05:53.050
each value via a key. Instead, in an array, we access it via its index position

128
05:53.050 --> 05:53.840
. Now, the

129
05:53.840 --> 05:57.440
position is going to start from position number zero. So in this case here,

130
05:57.440 --> 05:58.160
this will be position

131
05:58.160 --> 06:02.160
zero and then we just move on up. So nugget user is zero. Ansible is one john
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

131
05:58.160 --> 06:02.160
zero and then we just move on up. So nugget user is zero. Ansible is one john

132
06:02.160 --> 06:02.480
is two,

133
06:02.480 --> 06:07.020
samona is three and we have no more elements within the array. So if I want to

134
06:07.020 --> 06:08.240
target john,

135
06:08.240 --> 06:12.700
that is going to be index position two, therefore in my answerable playbook,

136
06:12.700 --> 06:13.440
what I'm going to do

137
06:13.440 --> 06:17.430
is after I call the Linux and then the user's key, this gives me access to my

138
06:17.430 --> 06:18.320
array object and

139
06:18.320 --> 06:22.460
then within square brackets, I'm going to specify the actual position within

140
06:22.460 --> 06:23.280
the array I want to

141
06:23.280 --> 06:27.460
access. So in this case here, I'm going to access position two. If I just save

142
06:27.460 --> 06:27.920
this,

143
06:27.920 --> 06:31.780
clear the screen, we run the playbook and look at the result that says here, I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

143
06:27.920 --> 06:31.780
clear the screen, we run the playbook and look at the result that says here, I

144
06:31.780 --> 06:32.880
have a user called

145
06:32.880 --> 06:36.400
john on my system, we were able to actually parse out that very particular

146
06:36.400 --> 06:37.680
piece of information.

147
06:37.680 --> 06:40.870
And if we just make the modification, change it to let's maybe say it less

148
06:40.870 --> 06:41.680
position zero,

149
06:41.680 --> 06:45.120
what that should now return is the first element within the array, which if we

150
06:45.120 --> 06:45.920
're under playbook,

151
06:45.920 --> 06:50.240
as we can see here, now passes out the value nugget user. So that's how we can

152
06:50.240 --> 06:50.800
effectively

153
06:50.800 --> 06:54.880
build out complex data within our variable files and access them via the

154
06:54.880 --> 06:56.080
corresponding key

155
06:56.080 --> 07:00.000
or in the case of an array via the corresponding index position.
