So now that we know what a basic YAML document looks like, what I want to show
you is how
we can create something known as key value pairs within YAML.
So with that said, let's just dive in.
Now one of the primary purposes of having these key value pairs is that it's
going to allow
you to make your data much more accessible programmatically.
Take for example this text document.
If you wanted some type of automation system to programmatically peel out the
data within
this particular paragraph, it's actually quite a complicated process.
You would have to use things such as regular expressions to really try to match WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
You would have to use things such as regular expressions to really try to match
out very
particular patterns within the text.
And the problem with this is that there can be multiple instances of those
patterns.
Say for example that we wanted to parse out my name.
Now if you happen to know that my name here is John McGovern, well it does make
the search
pattern a lot easier to find.
But that is if you know what you're looking for.
Say for example you have a whole bunch of configurations whereby you have lots
and lots
of data to deal with.
Quite simply this data is far too unstructured whereas if we happen to just WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
take this information
here and utilize these key value pairs, we can easily access the very data that
we want
very very precisely.
So say for example we could create a key called name and associated with that
key would be
the corresponding value.
So John McGovern in my case.
So if for example you want to retrieve the name from your data sets, we just
tell the
system to look for the name key and it will return the corresponding value in
this case
here, John McGovern as opposed to trying to rifle through all of this text un WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
here, John McGovern as opposed to trying to rifle through all of this text un
structured,
trying to find a very particular pattern match.
Once again this still might not be quite so clear just yet.
But once we begin getting hands on writing these Ansible Playbooks, you're
going to
see that having this very strict structure really does help us when we're
trying to engage
an automation system.
So let's look at some of the rules that we can use to create these key value
pairs.
Now before we actually dig in and begin writing these yaml files, the text
editor that I would
suggest you use is Vim.
The reason being is that when it sees that dot yml extension, it's going to WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The reason being is that when it sees that dot yml extension, it's going to
give you some
nice syntax highlighting and also going to help you follow the rules of the
indentation.
So in this case here, I'm just going to use Vim and I will just say example dot
yml.
Okay.
So if I press I for insert and have my three hyphens at the top, what I'm going
to do
is first create my key.
Now the way we specify a key is just by giving the key a name and then we
specify a colon.
Notice the coloration change here.
The text editor knows that we have just created the key.
Now one thing to note is that when we create these keys, we want to leave a WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now one thing to note is that when we create these keys, we want to leave a
space before
we pass in the value.
So if I say space and then John McGuffin, this is absolutely valid.
However, if I happen to just move over here and I remove the space, check out
the syntax
highlighting.
Suddenly it's when read again, this is no longer the key.
We do not have the key value pair.
So just be aware of that.
Like I say, Vim is going to help you.
Now when I press return, naturally, we're going to get some type of indentation
.
This happens to be two spaces in. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This happens to be two spaces in.
But for now, I don't actually want to use this indentation.
We'll talk about that very, very shortly.
So all I will do is just press backspace once and notice it actually goes back
two spaces
to the correct level of indentation.
So if I say age and then colon and then space, I can now pass in my age, which
in this case
here is actually 38.
Once again, I hit enter, we try to automatically indent.
We don't want to use this quite just yet.
So I just press backspace again.
Now roughly as a concept, what we're looking at here is that when we have a key WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now roughly as a concept, what we're looking at here is that when we have a key
, you can
roughly imagine that as like a folder and the value is what is actually inside
the folder.
Now the way I say this is because the actual value kind of belongs to the key,
so to speak,
the same way that anything inside of a folder belongs to that particular folder
.
This might make more sense when we happen to see nested values.
So say for example, look at this.
What if I also wanted to add in my address, okay?
Now inside of the address, I didn't just want to type in the entire address,
such as maybe
say one main street in Scotland, and then let's maybe say there's a postcode or WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
say one main street in Scotland, and then let's maybe say there's a postcode or
a zip code
in the US, which is the 54321.
Let's maybe say I wanted to break up all of this data into individual
components by
still wanting to all to be under the association of address.
Well, the way we can do this is just create more key value pairs, but indent it
within
the address key.
So look at this.
If I actually don't give a value right here and I just press enter, we get this
automatic
indentation.
And what I can just do is just create a new key. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
And what I can just do is just create a new key.
So I could say, for example, the street that I belong to and how do I create a
key?
Of course, I just say colon and now look at the highlighting we have created
another key
and it is indented within the address key.
So I could just say the street is one main street.
So the next key I create is a setting, which is going to be Glasgow in Scotland
.
And then I could just say, say, for example, the zip code, now it doesn't
actually make
sense in the UK.
We don't actually have zip codes, but nevertheless, you kind of get the drift.
The point here is though, is because we have actually indented all of this data WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
The point here is though, is because we have actually indented all of this data
here, we
know this data is effectively a child of the address.
Basically, all of this data here is directly related to the address.
It just has been subdivided and separated into more key value pairs.
So the way I happen to say this was kind of like having a folder on your
desktop.
Let me show you what that looks like.
So right now here, I have three folders.
If I go into the name folder and just double click it, we have the name John Mc
Govern inside.
If I go back out, I can go into age double click it. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If I go back out, I can go into age double click it.
We have the value 38.
If I go back out, if I select address.
Inside of address, we actually have more folders.
We have a street folder, city folder and a zip folder.
Just in the same way here, we have a street key, city key and a zip key.
Basically, all of this data here is contained within a greater address key.
This is a loaner to structure and associate data accordingly.
So if I now go into the street key or the street folder, if you wish, we see
the value
one main street.
If we go back out, we go into the city key or city folder, double click it, we WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
If we go back out, we go into the city key or city folder, double click it, we
see the
value is Glasgow and then finally, the zip key tells us that the zip value is 5
4321.
Like I said, the beauty of YAML is this human readability.
So you just correctly eyeballing this.
By virtue of seeing the indentation, your eyes and brain can easily interpret
all of
this information here as relating to and is subsequently a product of the
address information
that we're trying to communicate.
So whilst this might be a very frivolous example right now, when it comes to
creating configuration WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
creating configuration
files, you may have a whole bunch of nested data.
Say, for example, a key for servers and intended within that key, you may have
a database
key as well as a corresponding web key and then maybe some corresponding values
.
Let's maybe say eight dot eight dot eight dot eight and one dot one dot one dot
one.
This one here could be the IP address of the web server and this one here could
be the IP
address of the database server.
And again, just at a glance by virtue of the web and database keys being ind
ented with WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
ented with
then the greater server key, we know here that the database in the web happens
to be
talking about servers explicitly.
So the indentation and nesting allows us to correlate the data together.