WEBVTT

1
00:00.000 --> 00:04.080
So you may recall any previous skill we talked about how important YAML is in

2
00:04.080 --> 00:04.800
the world of

3
00:04.800 --> 00:09.280
network automation it is utilized say for example by Ansible and again this is

4
00:09.280 --> 00:09.920
one of the reasons

5
00:09.920 --> 00:15.380
why Ansible is so popular is because of its reliance on YAML because YAML is

6
00:15.380 --> 00:16.240
very very human

7
00:16.240 --> 00:20.430
readable. So as such many of our configuration files are going to be stored in

8
00:20.430 --> 00:21.520
the YAML format

9
00:21.520 --> 00:26.280
similarly if we want to perhaps create our own inventory and read it into

10
00:26.280 --> 00:27.440
Python very often

11
00:27.440 --> 00:31.520
the fill format we will be using is actually the YAML format so we want to have
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

11
00:27.440 --> 00:31.520
the fill format we will be using is actually the YAML format so we want to have

12
00:31.520 --> 00:32.800
a way to read

13
00:32.800 --> 00:37.360
in YAML files directly and natively within Python. Now there actually is a

14
00:37.360 --> 00:38.640
particular way in which

15
00:38.640 --> 00:43.960
we can do this and this is by using a specialized library known as PyYAML. Now

16
00:43.960 --> 00:44.960
in order to actually

17
00:44.960 --> 00:49.240
get our hands on PyYAML it doesn't actually come with Python directly so we're

18
00:49.240 --> 00:49.920
going to have to do

19
00:49.920 --> 00:54.130
some additional installation. Now before we actually do this let me just take a

20
00:54.130 --> 00:55.120
brief word to talk

21
00:55.120 --> 00:58.710
to about what is known as a virtual environment. So really all a virtual

22
00:58.710 --> 01:00.720
environment is as a way whereby
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

22
00:58.710 --> 01:00.720
environment is as a way whereby

23
01:00.720 --> 01:04.530
you can safely install particular libraries into your environment and it's not

24
01:04.530 --> 01:05.520
going to have any

25
01:05.520 --> 01:09.620
effect on your main system installations basically they're going to be isolated

26
01:09.620 --> 01:10.400
and separated

27
01:10.400 --> 01:14.170
and quite honestly it's just a very good practice to do so. So let me just show

28
01:14.170 --> 01:15.360
you how we can create

29
01:15.360 --> 01:19.370
a virtual environment within our directory here. Now because I want to be into

30
01:19.370 --> 01:20.080
what I will say is

31
01:20.080 --> 01:24.950
pseudo-opt install and what I will say is PyYAML 3-VenV if I hit Enter here and

32
01:24.950 --> 01:26.400
then type in my super

33
01:26.400 --> 01:29.570
user password and hit Enter game. What's going to happen is we're going to

34
01:29.570 --> 01:31.040
download the VenV package.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

34
01:29.570 --> 01:31.040
download the VenV package.

35
01:31.040 --> 01:34.370
This is going to allow me to create virtual environments. So now if I just say

36
01:34.370 --> 01:36.880
PyYAML 3-M for the module

37
01:36.880 --> 01:42.500
and then say VenV what I will now do is say dot VenV pretty much I'm saying pay

38
01:42.500 --> 01:44.080
PyYAML use the VenV

39
01:44.080 --> 01:48.660
module to create a virtual environment in this directory called dot VenV now

40
01:48.660 --> 01:49.680
dot just makes it

41
01:49.680 --> 01:52.860
hidden environment or rather hidden directory. So when I do it irregular

42
01:52.860 --> 01:53.840
listing you're not going

43
01:53.840 --> 01:57.720
to see this particular folder but if I do CD to change into that directory we

44
01:57.720 --> 01:58.720
actually see it does

45
01:58.720 --> 02:02.470
exist. Now inside this folder we're going to have some particular files namely
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

45
01:58.720 --> 02:02.470
exist. Now inside this folder we're going to have some particular files namely

46
02:02.470 --> 02:03.840
this one here the bin

47
02:03.840 --> 02:09.470
file rather the bin directory. If I just say source bin/activate on the left

48
02:09.470 --> 02:10.560
hand side here we're

49
02:10.560 --> 02:14.240
actually seeing that the virtual environment has now been activated so let me

50
02:14.240 --> 02:14.800
just go back

51
02:14.800 --> 02:18.300
to the directory and here we are right here but the thing is now I am working

52
02:18.300 --> 02:19.360
within a virtual

53
02:19.360 --> 02:23.210
environment meaning that anything that happened to install is not installed

54
02:23.210 --> 02:24.560
system-wide. So basically

55
02:24.560 --> 02:27.390
I've got a little bit more freedom I don't have to worry about this particular

56
02:27.390 --> 02:28.400
installation breaking

57
02:28.400 --> 02:33.180
my machine. So the way we can actually install things in PyThon is by using the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

57
02:28.400 --> 02:33.180
my machine. So the way we can actually install things in PyThon is by using the

58
02:33.180 --> 02:34.560
pip package manager.

59
02:34.560 --> 02:40.400
So what I was first say is Sido apt install PyThon3-Pip and to enter. Now as it

60
02:40.400 --> 02:41.600
transpires I have already

61
02:41.600 --> 02:46.390
got pip installed so what I can now do is they pip3 install and I'm going to

62
02:46.390 --> 02:47.680
install PyYaml. So now

63
02:47.680 --> 02:52.030
we can see here I have PyYaml and what this now means is that I can import this

64
02:52.030 --> 02:53.120
library into my

65
02:53.120 --> 02:57.530
scripts and make use of it. So let me just create a new file let's just call

66
02:57.530 --> 03:00.240
this about me.Yaml
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

66
02:57.530 --> 03:00.240
this about me.Yaml

67
03:00.240 --> 03:04.190
okay really great simple so if I open this up right here I'm going to basically

68
03:04.190 --> 03:05.200
describe myself

69
03:05.200 --> 03:10.640
utilizing Yaml. I'll say my name is John McGovern. I'll say my occupation as a

70
03:10.640 --> 03:13.440
trainer. So my age is

71
03:13.440 --> 03:17.440
38 and I'll say my hobbies and I'm going to have more than one hobby so what I

72
03:17.440 --> 03:18.160
will do is use this

73
03:18.160 --> 03:22.550
particular syntax with these hyphens. So I'll say I like to do programming, I

74
03:22.550 --> 03:23.920
like to do networking,

75
03:23.920 --> 03:28.940
I like to golf, I also enjoy movies and you know that'll do that kind of sums

76
03:28.940 --> 03:30.080
me up okay
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

76
03:28.940 --> 03:30.080
me up okay

77
03:30.080 --> 03:33.260
now right now the data here is fairly trivial it doesn't matter what it is this

78
03:33.260 --> 03:33.840
could be network

79
03:33.840 --> 03:37.630
configurations this could be device information to connect to. The point is is

80
03:37.630 --> 03:38.800
that the information

81
03:38.800 --> 03:43.610
is in the YAML format and we want to be able to get access to this information

82
03:43.610 --> 03:44.880
utilizing Python.

83
03:44.880 --> 03:50.610
So what I will do here is just say example 2.py let's open this up and let's

84
03:50.610 --> 03:51.360
see how we can get

85
03:51.360 --> 03:55.370
access to this particular data. So here's the first thing I'm going to do I'm

86
03:55.370 --> 03:57.040
going to import

87
03:57.040 --> 04:01.200
YAML and this is the name of the library that I just installed. Now very very
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

87
03:57.040 --> 04:01.200
YAML and this is the name of the library that I just installed. Now very very

88
04:01.200 --> 04:02.080
similar to what we

89
04:02.080 --> 04:06.070
saw before I'm going to use my context manager so you remember how we do this

90
04:06.070 --> 04:07.200
we say with and then

91
04:07.200 --> 04:11.470
I'll use the open function and this time though what I'm going to do is specify

92
04:11.470 --> 04:12.640
the name of the YAML

93
04:12.640 --> 04:16.970
file that I want to open so in this case here it's just called about dashme.yam

94
04:16.970 --> 04:17.600
l now what I'm

95
04:17.600 --> 04:22.020
going to be doing I'm going to be opening then read mode so I'll say comma then

96
04:22.020 --> 04:22.880
R and then I will

97
04:22.880 --> 04:26.100
use a variable name so I gain let's just stick with the same table format let's

98
04:26.100 --> 04:27.120
just say target file

99
04:27.120 --> 04:30.930
okay now this is where things get a little bit different let's just say I want
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

99
04:27.120 --> 04:30.930
okay now this is where things get a little bit different let's just say I want

100
04:30.930 --> 04:32.000
to create a variable

101
04:32.000 --> 04:35.710
called my data to store the actual information when we read it what I don't

102
04:35.710 --> 04:36.720
want to do is simply say

103
04:36.720 --> 04:41.350
you know targetfile.read in this case we don't want to do that let me show you

104
04:41.350 --> 04:42.480
why if I say print

105
04:42.480 --> 04:47.370
my data here and I save this and I run the script well it looks like we got

106
04:47.370 --> 04:48.880
access to the data

107
04:48.880 --> 04:54.140
but we really didn't this here is just once again regular string data in fact I

108
04:54.140 --> 04:54.880
can just prove this

109
04:54.880 --> 04:59.500
here just by printing the type so let's just save this try it again as we can

110
04:59.500 --> 05:00.800
see it's just a regular
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

110
04:59.500 --> 05:00.800
see it's just a regular

111
05:00.800 --> 05:03.960
string and the problem here is that the reason why we've actually used a

112
05:03.960 --> 05:05.200
particular file format

113
05:05.200 --> 05:08.980
such as YAML is because YAML actually gives us access to structured data

114
05:08.980 --> 05:10.160
meaning that we can

115
05:10.160 --> 05:13.740
work with the data programmatically now as it stands right now if we just read

116
05:13.740 --> 05:14.880
it as regular text

117
05:14.880 --> 05:19.180
we cannot do this we want to have this ability to associate things like keys

118
05:19.180 --> 05:20.960
with particular values

119
05:20.960 --> 05:25.090
and have the concept of things like list objects okay now right now as it

120
05:25.090 --> 05:26.800
stands all of this is just

121
05:26.800 --> 05:31.780
being associated or rather interpreted as one big string not all of these
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

121
05:26.800 --> 05:31.780
being associated or rather interpreted as one big string not all of these

122
05:31.780 --> 05:33.440
individual objects we

123
05:33.440 --> 05:37.690
don't have a difference between this here being a string and this here being an

124
05:37.690 --> 05:39.120
integer for example

125
05:39.120 --> 05:43.020
so we want to make sure that we load it and not print it as a string but load

126
05:43.020 --> 05:44.320
it and print it as a

127
05:44.320 --> 05:48.060
YAML document so the way we're going to do this is by making use of this

128
05:48.060 --> 05:49.520
particular import

129
05:49.520 --> 05:53.930
right here YAML account so what I will now say is I will take the YAML library

130
05:53.930 --> 05:54.640
and I'm going to use

131
05:54.640 --> 05:58.820
what is known as the safe load functions is going to allow me to safely load my

132
05:58.820 --> 05:59.440
YAML file

133
05:59.440 --> 06:02.130
and what I'm going to be loading I'm going to be loading the target file that I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

133
05:59.440 --> 06:02.130
and what I'm going to be loading I'm going to be loading the target file that I

134
06:02.130 --> 06:03.120
've just opened

135
06:03.120 --> 06:08.240
so now we have used this particular library to load the information in this

136
06:08.240 --> 06:09.680
file and save it as

137
06:09.680 --> 06:14.640
this particular object right here my data so if I now print this save you are a

138
06:14.640 --> 06:15.680
up try again

139
06:15.680 --> 06:19.280
notice the way this prints out a little bit differently now the reason why is

140
06:19.280 --> 06:19.920
that if we say for

141
06:19.920 --> 06:24.460
example print the type of the data right here what's what happens okay it's not

142
06:24.460 --> 06:25.680
going to be a string

143
06:25.680 --> 06:29.710
we're actually getting a Python dictionary because what we actually have here

144
06:29.710 --> 06:30.640
are all of these
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

144
06:29.710 --> 06:30.640
are all of these

145
06:30.640 --> 06:34.740
Python dictionary keys so the cool thing about this therefore means it can

146
06:34.740 --> 06:36.000
still access this data

147
06:36.000 --> 06:40.240
programmatically by referencing the key so say for example I just want to very

148
06:40.240 --> 06:42.160
particularly target

149
06:42.160 --> 06:47.260
the age information within this document like do it as opposed to printing out

150
06:47.260 --> 06:48.480
the entire document

151
06:48.480 --> 06:51.520
within my square brackets I can just give the name of the key that I want to

152
06:51.520 --> 06:52.640
get say for example the

153
06:52.640 --> 06:57.110
age if I save this run the script look at this right there I can now very

154
06:57.110 --> 06:58.560
precisely target and

155
06:58.560 --> 07:02.870
parse out very particular details of this data which as you can imagine is very
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

155
06:58.560 --> 07:02.870
parse out very particular details of this data which as you can imagine is very

156
07:02.870 --> 07:03.680
very important

157
07:03.680 --> 07:08.850
when we want to target and send very specific pieces of our configuration this

158
07:08.850 --> 07:09.360
is why we would

159
07:09.360 --> 07:13.810
like to have it in this structure format in fact if I do things such as you

160
07:13.810 --> 07:15.920
know age is equal to

161
07:15.920 --> 07:22.540
my data and grab the age key if I print the type of the age save this run it

162
07:22.540 --> 07:23.680
again but actually

163
07:23.680 --> 07:27.240
seeing this as an integer value so what that means therefore is that we could

164
07:27.240 --> 07:28.160
run mathematical

165
07:28.160 --> 07:34.180
operations I can say if the age is greater than 18 for example what I can do is
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

165
07:28.160 --> 07:34.180
operations I can say if the age is greater than 18 for example what I can do is

166
07:34.180 --> 07:35.600
print statement

167
07:35.600 --> 07:41.330
this person can go to bars save this run the script as we can see here this

168
07:41.330 --> 07:42.800
person can go to bars

169
07:42.800 --> 07:47.850
whereas if we change the value let's put the person as 8 for example as opposed

170
07:47.850 --> 07:49.120
to 38 if I

171
07:49.120 --> 07:53.710
rerun the script as we can see it does not print out the statement we can go to

172
07:53.710 --> 07:54.400
bars because we

173
07:54.400 --> 07:58.630
didn't pass the condition so really whilst this particular example is kind of

174
07:58.630 --> 07:59.840
frivolous the general

175
07:59.840 --> 08:04.610
point is very important having this data stored in YAML allows us to store it
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

175
07:59.840 --> 08:04.610
point is very important having this data stored in YAML allows us to store it

176
08:04.610 --> 08:05.840
in a programmatic

177
08:05.840 --> 08:10.190
fashion which means that when we access the data within our Python scripts we

178
08:10.190 --> 08:11.440
can access it as

179
08:11.440 --> 08:15.760
programmatic objects not just as a general string like just reading a text file

180
08:15.760 --> 08:16.480
we can read things

181
08:16.480 --> 08:19.990
like dictionaries we can have access to list of objects we can have access to

182
08:19.990 --> 08:21.520
strings within that

183
08:21.520 --> 08:25.420
as well as integers so if we want to perform mathematical operations all of

184
08:25.420 --> 08:26.640
this is available to us

185
08:26.640 --> 08:30.870
by using the correct libraries which is the type of thing we need when we want
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

185
08:26.640 --> 08:30.870
by using the correct libraries which is the type of thing we need when we want

186
08:30.870 --> 08:32.240
to say for example

187
08:32.240 --> 08:37.250
send real configurations to real devices or perform network configuration tests

188
08:37.250 --> 08:37.840
against real

189
08:37.840 --> 08:41.760
devices so now we've covered some of the core fundamentals of reading files and

190
08:41.760 --> 08:42.320
reading and

191
08:42.320 --> 08:46.430
potential configuration files let's now have a little bit more fun and begin

192
08:46.430 --> 08:47.760
sending actual commands

193
08:47.760 --> 08:49.620
to your devices utilizing Python.
