Okay friends, so let's talk about YAML.
Now all YAML is is a data serialization language.
And the big thing about this particular file format
is that we have an emphasis on human readability.
So maybe perhaps you are familiar with other file formats
such as JSON or perhaps even XML.
And ultimately they allow us to do a similar thing with data.
The data is just presented a different way
and each of these have their own rule sets.
So how about we discuss some of the rules behind YAML
as well as see some examples WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
as well as see some examples
so that we can actually see what this really looks like.
Okay, so the very first thing to say
and this is very, very important to understand
is that YAML is a white space sensitive.
Reason being is that with respect to YAML
we have a very big emphasis on the concept of indentation.
So when we begin to explore some examples
you're going to see that we can actually associate
particular elements within a YAML document together
using this concept of indentation.
And if you get the indentation wrong
the association between different elements WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
the association between different elements
can become confused and in some cases
just become completely invalid.
So really, really, really be aware
of the type of indentation that you use.
Now with respect to YAML
you can use four spaces indentation.
This is fairly commonly used
but the general convention the most common way
is in fact to use two spaces.
Now there is no hard and fast rule
that you have to use one or the other.
These are the more common implementations
like I say with two spaces being the most popular. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
like I say with two spaces being the most popular.
As such because of this convention
I myself prefer to use two spaces
that just keeps things a little bit neater.
So in my particular examples
that is the convention I'm going to follow.
Now whilst it's not important
if you happen to use two or four spaces indentation
the main thing we want to remember
is that we do not want to mix and match them.
You know use four spaces for one part of the document
and then suddenly start using two.
This is absolutely not what we want to do.
Basically pick something that you like
and just stick with it. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and just stick with it.
Now also to be clear you may have the temptation
to use tabs for your indentation
but realistically the rules of YAML specify
that you should be using spaces instead
so actually use that space bar.
Now if you happen to be absolutely adamant on using tabs
you just find it quicker to get that indentation.
You can still use a tab key
but what I suggest you do is you configure your text editor
to actually convert the tabs into spaces that way
even if you press the tab to get quick indentation
ultimately under the hood it's going to be translated WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
ultimately under the hood it's going to be translated
and converted into spaces
that will keep things nice and consistent
with the expected rules of YAML.
Now another thing to note in YAML
is that we happen to denote the start of a YAML document
with three hyphens.
So when we happen to be writing our Ansible Playbooks
this is what you can expect to see
at the beginning of a particular play.
Just to be clear though this is not actually
an enforced convention you can absolutely create
a YAML document without these three hyphens.
This is just a general standard
used to denote the start of a YAML document.
In fact if you want to have multiple YAML documents WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
In fact if you want to have multiple YAML documents
within the same file so to speak
you can break up that document
ultimately creating these new YAML Paragraphs so to speak
by using these three hyphens
that is going to separate them out.
Now another big advantage of YAML
and one of the reasons why it's very very popular
for configuration files is that allows us to use
this hash symbol.
Now the use of the hash symbol itself
isn't actually a revelation.
Rather what I'm talking about is what this hash symbol
allows us to do, it allows us to create comments.
Now if you are familiar with the JSON format WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
Now if you are familiar with the JSON format
JSON is rather human readable
but one of the big downfalls within JSON
is that you do not have the ability for comments.
Now one of YAML's primary use cases
is for creating configuration files
having the ability to pair a configuration
with a particular comment beside it
giving the reader some type of context
about what this configuration is actually supposed to do.
This is one of the big ones that we get with the YAML format.
Another thing that we should be aware of
is that YAML files are going to end in the extension
either .YML or .YAML. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
either .YML or .YAML.
Now generally speaking the more common use
is to just say .YAML.
However for whatever reason and the Ansible Ecosystems fear
we tend to use .YML so as such
this is going to be the convention
that I'm going to follow within this particular course.
Just be aware that this is not mandatory
if you do see .YAML, do not freak out
it still is absolutely fine and accepted.
So let's take a look at an example YAML documents.
So here's a tip, when working with YAML WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So here's a tip, when working with YAML
one of the best things to do is to make use
of an online YAML parser.
Pretty much what these will do
is they will programmatically assess your YAML documents
and if you have any type of errors
perhaps you have an indentation error
perhaps you're using some type of invalid character.
Whatever it is, this particular parser here
or any parser online for example
will be able to detect that problem
and point out how to remedy it.
So really when you're writing playbooks
this can save you quite a lot of time.
So if we just collect one of these parsers WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
So if we just collect one of these parsers
let's maybe go with YAML parser online right here
on code beautify.
So on the left hand side here
what I've done is pasted in a very simple YAML document
that I created.
Notice at the very top of the document
we have our three hyphened right here
the noting that this is the beginning of a new YAML doc.
And then we have some information
to see the name and the corresponding value
as John McGovern.
We have an age of 99 which is not my actual age
thankfully and we have an email
and the corresponding value we have UK citizen WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
and the corresponding value we have UK citizen
and then we have something called true.
This happens to be known as a billion value.
This is supported in YAML
which pretty much allows us to have this type
of binary logic as something true
or as something false we'll get to see more of this
later on throughout this course.
Just to understand that we can have a regular text
or sentences so to speak.
These are known as strings.
We can actually have numerical values here
which would be known as integers.
These are your numbers.
And we have these billion values here.
Now there actually are more data types
that we can specify within YAML. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
that we can specify within YAML.
We'll talk about more of that in an upcoming video.
And we also have some information
about what skills I can do.
And really this is what a very simple YAML document
looks like.
Now of course as the data becomes more complex,
becomes more populated,
things can be a little bit more complicated to look at.
But really so long as you understand
the actual rows of YAML,
you can map out all this data programmatically.
So this could be a file for the configurations
that you want to have across all of your servers
within a network.
By following the rows of YAML,
we can have this information, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
we can have this information,
any standardized and programmatic way
that is consistent.
And as we can see here,
fairly easy to read for a human.
You may not be great at programming
but I'm pretty sure you can understand
and interpret what we're actually seeing here
on the left hand side.
Now the cool thing like I said about things
like these YAML parsers,
is there allows us to see where you may have went wrong.
So say for example,
if I remove the three hyphens,
notice that the YAML document is still actually valid.
Well, they may be visually instructive.
They're not actually necessary
to construct the YAML documents. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
to construct the YAML documents.
However, what if I happen to do something like say,
this in the document,
notice on the left hand side,
we have this little X telling us
that we have some type of error.
And it tells us that we have now introduced
an unknown tag into the document.
And we can see here on the right hand side,
the rendered value is now simply null,
as opposed to the value of the name.
Similarly, if I just happened to start typing
in some absolute garbage into the document right here,
look at this on the right hand side,
we can clearly see that we have an error.
This is not valid YAML at all. WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
This is not valid YAML at all.
And we can actually see here,
the line number where the problem is introduced,
line number three.
So we know starting right here,
this is where the problem begins, so if we remove this,
we can see we still have a problem on line four.
So we remove line four,
we see we have a problem on line five,
if we remove line five and hit enter,
suddenly that problem has been fixed.
And if we resolve the problem here of the unknown tag,
remove that, suddenly once again,
the YAML document is valid.
Definitely, I would encourage you to make use
of these YAML tools.
It's going to make your life much, much easier
because whilst YAML is very human readable, WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000
because whilst YAML is very human readable,
it is notoriously easy to make a mistake
in a YAML document
because of the whites based sensitivity
and the emphasis on indentation levels.