So you may recall any previous skill we talked about how important YAML is in
the world of
network automation it is utilized say for example by Ansible and again this is
one of the reasons
why Ansible is so popular is because of its reliance on YAML because YAML is
very very human
readable. So as such many of our configuration files are going to be stored in
the YAML format
similarly if we want to perhaps create our own inventory and read it into
Python very often
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
the fill format we will be using is actually the YAML format so we want to have
a way to read
in YAML files directly and natively within Python. Now there actually is a
particular way in which
we can do this and this is by using a specialized library known as PyYAML. Now
in order to actually
get our hands on PyYAML it doesn't actually come with Python directly so we're
going to have to do
some additional installation. Now before we actually do this let me just take a
brief word to talk
to about what is known as a virtual environment. So really all a virtual
environment is as a way whereby WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
environment is as a way whereby
you can safely install particular libraries into your environment and it's not
going to have any
effect on your main system installations basically they're going to be isolated
and separated
and quite honestly it's just a very good practice to do so. So let me just show
you how we can create
a virtual environment within our directory here. Now because I want to be into
what I will say is
pseudo-opt install and what I will say is PyYAML 3-VenV if I hit Enter here and
then type in my super
user password and hit Enter game. What's going to happen is we're going to
download the VenV package. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
download the VenV package.
This is going to allow me to create virtual environments. So now if I just say
PyYAML 3-M for the module
and then say VenV what I will now do is say dot VenV pretty much I'm saying pay
PyYAML use the VenV
module to create a virtual environment in this directory called dot VenV now
dot just makes it
hidden environment or rather hidden directory. So when I do it irregular
listing you're not going
to see this particular folder but if I do CD to change into that directory we
actually see it does
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
exist. Now inside this folder we're going to have some particular files namely
this one here the bin
file rather the bin directory. If I just say source bin/activate on the left
hand side here we're
actually seeing that the virtual environment has now been activated so let me
just go back
to the directory and here we are right here but the thing is now I am working
within a virtual
environment meaning that anything that happened to install is not installed
system-wide. So basically
I've got a little bit more freedom I don't have to worry about this particular
installation breaking
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
my machine. So the way we can actually install things in PyThon is by using the
pip package manager.
So what I was first say is Sido apt install PyThon3-Pip and to enter. Now as it
transpires I have already
got pip installed so what I can now do is they pip3 install and I'm going to
install PyYaml. So now
we can see here I have PyYaml and what this now means is that I can import this
library into my
scripts and make use of it. So let me just create a new file let's just call
this about me.Yaml WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
this about me.Yaml
okay really great simple so if I open this up right here I'm going to basically
describe myself
utilizing Yaml. I'll say my name is John McGovern. I'll say my occupation as a
trainer. So my age is
38 and I'll say my hobbies and I'm going to have more than one hobby so what I
will do is use this
particular syntax with these hyphens. So I'll say I like to do programming, I
like to do networking,
I like to golf, I also enjoy movies and you know that'll do that kind of sums
me up okay WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
me up okay
now right now the data here is fairly trivial it doesn't matter what it is this
could be network
configurations this could be device information to connect to. The point is is
that the information
is in the YAML format and we want to be able to get access to this information
utilizing Python.
So what I will do here is just say example 2.py let's open this up and let's
see how we can get
access to this particular data. So here's the first thing I'm going to do I'm
going to import
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
YAML and this is the name of the library that I just installed. Now very very
similar to what we
saw before I'm going to use my context manager so you remember how we do this
we say with and then
I'll use the open function and this time though what I'm going to do is specify
the name of the YAML
file that I want to open so in this case here it's just called about dashme.yam
l now what I'm
going to be doing I'm going to be opening then read mode so I'll say comma then
R and then I will
use a variable name so I gain let's just stick with the same table format let's
just say target file
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
okay now this is where things get a little bit different let's just say I want
to create a variable
called my data to store the actual information when we read it what I don't
want to do is simply say
you know targetfile.read in this case we don't want to do that let me show you
why if I say print
my data here and I save this and I run the script well it looks like we got
access to the data
but we really didn't this here is just once again regular string data in fact I
can just prove this
here just by printing the type so let's just save this try it again as we can
see it's just a regular WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
see it's just a regular
string and the problem here is that the reason why we've actually used a
particular file format
such as YAML is because YAML actually gives us access to structured data
meaning that we can
work with the data programmatically now as it stands right now if we just read
it as regular text
we cannot do this we want to have this ability to associate things like keys
with particular values
and have the concept of things like list objects okay now right now as it
stands all of this is just
being associated or rather interpreted as one big string not all of these WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
being associated or rather interpreted as one big string not all of these
individual objects we
don't have a difference between this here being a string and this here being an
integer for example
so we want to make sure that we load it and not print it as a string but load
it and print it as a
YAML document so the way we're going to do this is by making use of this
particular import
right here YAML account so what I will now say is I will take the YAML library
and I'm going to use
what is known as the safe load functions is going to allow me to safely load my
YAML file
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
and what I'm going to be loading I'm going to be loading the target file that I
've just opened
so now we have used this particular library to load the information in this
file and save it as
this particular object right here my data so if I now print this save you are a
up try again
notice the way this prints out a little bit differently now the reason why is
that if we say for
example print the type of the data right here what's what happens okay it's not
going to be a string
we're actually getting a Python dictionary because what we actually have here
are all of these WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
are all of these
Python dictionary keys so the cool thing about this therefore means it can
still access this data
programmatically by referencing the key so say for example I just want to very
particularly target
the age information within this document like do it as opposed to printing out
the entire document
within my square brackets I can just give the name of the key that I want to
get say for example the
age if I save this run the script look at this right there I can now very
precisely target and
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
parse out very particular details of this data which as you can imagine is very
very important
when we want to target and send very specific pieces of our configuration this
is why we would
like to have it in this structure format in fact if I do things such as you
know age is equal to
my data and grab the age key if I print the type of the age save this run it
again but actually
seeing this as an integer value so what that means therefore is that we could
run mathematical
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
operations I can say if the age is greater than 18 for example what I can do is
print statement
this person can go to bars save this run the script as we can see here this
person can go to bars
whereas if we change the value let's put the person as 8 for example as opposed
to 38 if I
rerun the script as we can see it does not print out the statement we can go to
bars because we
didn't pass the condition so really whilst this particular example is kind of
frivolous the general
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
point is very important having this data stored in YAML allows us to store it
in a programmatic
fashion which means that when we access the data within our Python scripts we
can access it as
programmatic objects not just as a general string like just reading a text file
we can read things
like dictionaries we can have access to list of objects we can have access to
strings within that
as well as integers so if we want to perform mathematical operations all of
this is available to us
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
by using the correct libraries which is the type of thing we need when we want
to say for example
send real configurations to real devices or perform network configuration tests
against real
devices so now we've covered some of the core fundamentals of reading files and
reading and
potential configuration files let's now have a little bit more fun and begin
sending actual commands
to your devices utilizing Python.