🚀 Welcome, Pythoholic tribe! Let's get started with YAML for beginners, and learn how to write an Ansible playbook. This video introduces Ansible Modules and offers an Ansible tutorial for beginners, perfect for those interested in automation and configuration management.
🎯 Chapters: 00:00 - Intro 00:22 - What is YAML? 02:03 - Why to use YAML? 04:44 - YAML Structure 05:44 - Importance of Indentations 07:27 - Types in YAML
Support our mission: 💙 INSTAMOJO: https://instamojo.com/@pythoholic 💙 PAYPAL: paypal.me/pythoholic 💙 Check out the tech I use and recommend: www.amazon.in/shop/pythoholic
Connect and follow for more learning resources: 🔔 Podcast (Blabber Cloud): http://tinyurl.com/Pythoholic 🔔 YouTube: tinyurl.com/PythoholicYT
🚨Disclaimer: The content provided on this channel is independent and not affiliated with any organization. Our videos are based on our knowledge and understanding of the topic, and we recommend viewers conduct their own research to gain a fuller understanding. We're always seeking to improve, and your feedback helps us do that! Support the channel for more content like this in the future.
Hey everyone, before we dive into today's Ansible session, I have something exciting to share.
I am launching a complete mastering Ansible for beginners course very soon,
and there's a link in the description to join my waiting list, and you will get early access,
exclusive discounts, and be notified the moment it goes live. Plus, I love your feedback on what
you want to see in the course. The playbook that we write in Ansible are a combination of tasks
and that we want to accomplish or execute on the servers or a virtual machine.
But the playbook itself is written in a set of instructions, and we write a playbook with
YAML. So, YAML is the whole identity of our playbook, as every language or script has a
file name and an extension. YAML also has its extension as .yml or yaml. So, that is playbook.yml.
So, we pronounce it as yaml, even though it is spelled as yml or yaml. YAML is the blueprint
language of choice for its readability and simplicity. It's a data serialization language
that is used to input data for software applications. Now, that might sound a bit
technical. So, let's simplify it. So, think of YAML as a set of instructions you would give to
someone on their first day at work, except in this case that someone is Ansible, and the work is
configuring and managing servers. And YAML is often used to write configuration files.
But you might ask why? Because it's structured in a way that's intuitive for humans to read and
write, and at the same time, precise enough for computers to understand. In our Ansible playbook,
we use YAML to define tasks, set variables, and outline the roles and dependencies of our
services. So, this isn't just writing code. It's like crafting a story that Ansible reads to
bring our infrastructure to life. And the steps that we write in our playbook can be executed
across multiple servers with the help of the inventory file. Now, let's understand how we can
use YAML. And for that, we need to understand Jenny's perfect cup of coffee once again. To make
her coffee, she needs the raw ingredients and the predefined steps like grinding, boiling, filtering.
And all these have a perfect sense of becoming a type of data structure, like a list or a dictionary.
For now, keep your mind totally away from all the programming languages and computational terms.
Just imagine now that we are here for shopping. For that, we need a list of ingredients, isn't it?
So, here we have our raw materials list. So, in our list, we have coffee, flavoring, milk, water,
and sugar. And we have a key and a value mapping in our ingredients and the brands that we need
to purchase. So, these type of data structures are called dictionaries. And in a dictionary,
remember that you will have a key that will hold a specific value. So, here we have specific brands
attached to each of these ingredients. So, we have coffee for Peet's, the flavor is hazelnut,
milk is Dean's, water is normal, sugar is Domino. So, these are all the brands that are associated
with these raw materials. And then once we have the list of ingredients, we have another list
with the procedure or steps or what we call processes like grinding, pressing, boiling, etc.
And when we combine all these data sets, now we get the actual pseudo steps for making the coffee.
And we have a combined data set of a single dictionary of the process. So, when you think
of computation, we have to remember that a list is a combination of heterogeneous elements.
So, it could be a combination of strings or integers, or it could be both of them.
And a dictionary is a key value pair, where we have an index field, which we call as a key,
which holds the specified values or the specific values. And it could be a string or integer,
or it could be a list as well. So, remember these points very carefully. I know most of you
are well aware of these concepts, but we have to discuss this because we might have some of
our friends who may not be aware of these data types. So, if you can see here, we have the raw
materials list. So, you have coffee, flavored liquid, milk, water, sugar. We have the process
list, which is basically grinding, pressing, boiling, filtering, mixing. And we have a
where we have the key as raw materials and the value will be brand. And now we have the
combination of both, where we have ingredients and the list of ingredients that I have within
a particular dictionary. So, here as well, we have grinding, boiling, mixing, and within which
we have all the steps listed under that. So, this is basically a dictionary. This is basically a
list. These two are basically a list. So, now we have a whole data structure in place. So,
here, if you see, we have the list of raw materials, the raw materials is our key.
And the value that you see here becomes a list of values. And if you have used JSON before,
which is JavaScript object notation, and it's more like a dictionary. So, the list of values
that you have coffee, flavored liquid, milk, water, sugar, they are basically a part of the list.
But the key here that you see here is raw underscore materials. And the way we represent
enamel is you won't have any curly braces, but only a string, which is our key. And the hyphen
here, it is very important, which represents a value in a list. If you see here, we have
raw materials colon, and after that hyphen and the value that we want. So, this hyphen actually
represents that each of these terms that you see here become a part of that particular list
within raw materials. Now, let's see this example here. And we'll understand the importance of
spacing and indentation. So, if you can see, we have ingredients key within which we have three
values that is a list values that are coffee, flavored liquid and milk. But you can see a
difference here with the indentation. Within milk, we have hyphen dominoes, hyphen low fat, hyphen
toned. So, this basically it's inner list. So, this whole thing, these six values don't come
under the same list. So, within milk, we have another list, which basically has these three
values. So, how does it look like? So, you can see, we have the ingredients here. Within this,
we have coffee, flavor and milk. Next, we have within milk, we have domino, low fat and toned.
So, the importance of space is really, really, really critical. Because if you have one space
here and there, it will change the whole idea of what we are trying to write. And that is what I'll
show you now. So, let's suppose I made a mistake and the inner list is basically now dominoes,
low fat, toned. But I have made a mistake and I have kept it in line with all three values that
I have previously. So, now let's see what happens. So, within ingredients, we have exactly like I
showed you before, coffee, flavor and milk. Then, the low fat and domino still are present within
milk because they have the same indentation. But the tone that we had here now is within ingredients
and not under milk. And that is what happens when you mess with the spaces and indentations while
writing your YAML files. So, remember, spaces and indentations are really, really important.
And YAML is designed to be easily readable by humans and is often used for configuration files.
And in YAML, booleans represent true or false values and can be expressed in several ways
for flexibility. For instance, you have yes or no, true or false, or even capital true,
capital false. So, you can write it anyway, yes or no, or true or false. So, each of these represent
a boolean value. The next thing is also a very important concept that is the multi-line statement
in YAML. So, YAML supports multi-line statements. So, there are two ways to do this. So, one is
literal block scaler and the other one is folder block scaler. When it comes to literal block
scaler, using the vertical bar that you see, you can include new line characters within your
strings. This method preserves new lines and any trailing spaces. When it comes to folded block
scaler, so this is indicated by the greater than symbol or the sign. This method actually allows
for new lines to be replaced by spaces, essentially folding the text into a single line. So, let's
understand these terms in a more simple way. So, the first one that we have literal scaler block
or the literal block scaler, which is denoted by the pipe symbol. So, imagine you're writing a
recipe or a poem and the layout of the lines is important. So, by using a vertical bar, which
looks like a pipe in YAML, you can write your recipe or poem exactly how you want it to appear
with all the line breaks and spaces at the end of the lines kept intact. So, it's like pressing enter
at the end of a new line and expecting it to look the same way when someone else reads it.
I hope you got the point. Simple, isn't it? But when it comes to folded block scaler,
which is denoted by the greater than symbol, suppose you're writing a letter that's going
to be put on a poster and you don't want it to go off the edge. So, you let the words wrap around
the next line wherever you need to. So, by using a greater than sign, you tell YAML to take your
letters and make it flow like a paragraph with all the lines joined together smoothly. It's like
taking away all the enter keys you pressed and letting the words find their own place
to start a new line. So, in simple terms, use the vertical bar when you want to keep the format
exactly as you typed it like poetry and use the greater than sign when you're okay with the words
wrapping around like in a paragraph in a book or an essay. Now, let's talk about a simple but
important thing that are comments, expressions and quotes. So, in YAML files, any text that you
don't want the computer to read, there's a comment. I think this is exactly like in any
other language, but I am saying this in context of YAML. So, you make a comment by putting a hash
symbol, hash. We all know, we have used, we know what is a hash at the start of the line and
everything on that line after the hash is ignored by YAML, which is useful for adding notes or
explanations to the code. Next, we have expressions. So, expressions are like shortcuts
that tell our YAML file to get information, to get information from somewhere else. For example,
if you have a variable with a value assigned somewhere in your code, you can use that
variable in YAML by wrapping it with two curly braces like this. This is handy when you want
to use the same value in multiple places or if the value might change later. And then we have
quotes. In YAML, quotes are used to make sure that the computer understands exactly what you
mean. So, if you put text in a single quote here, YAML will take it exactly as it is, even if it
has characters that are normally special. But if you use double quotes, you will include special
characters like a new line slash n or a tab slash t by using a backslash just before the character.
So, for simple, you will use a single quote, but for, if you want to include special characters
as well, you can use double quotes.