Instructor: Hello, and welcome to this lecture. In this lecture, we take a look at what YAML files are. All Ansible Playbooks are written in YAML. If you are familiar with YAML already, feel free to skip this section and head over to the next section. If you have not worked with YAML in the past, I would highly recommend going through this because the rest of the course depends entirely on YAML. Ansible Playbooks are text files or configuration files rather, that are written in a particular format called YAML. If you have worked with other data structure formats like XML or JSON, you should be able to easily pick it up. Don't worry if you haven't worked on any of these, you should still be able to easily pick it up going through the coding exercises that accompany this course. A YAML file is used to represent data, in this case, configuration data. Here is a quick comparison of a sample data in three different formats. The one on the left is XML where we display a list of servers and their information. The same data is represented in JSON format in the middle and finally in YAML format to the right. Take a minute to compare the three formats. Let's take a close look at YAML. If you take the data in its simplest form such as key value pair, this is how you would define it in YAML. Key and value separated by a colon. The keys are fruit, vegetable, liquid, and meat and the values are apple, carrot, water, and chicken. Remember, you must have a space followed by colon differentiating the key and the value. Let's take a look at how an array is represented. We would like to list some fruits and vegetables. We would say fruits followed by a colon. On the next line, enter each item with a dash in the front. The dash indicates that it's an element of an array. How about a dictionary? A dictionary is a set of properties grouped together under an item. Here, we try to represent nutrition information of two fruits. The calories, fat, and carbs are different for each fruit. Notice the blank space before each item. You must have equal number of blank spaces before the properties of a single item so they are all aligned together. Let's take a closer look at spaces in YAML. Here we have a dictionary representing the nutrition information of banana. The total amount of calories, fat, and carbs are shown. Notice the number of spaces before each property that indicates these key value pairs fall within banana. But what if we had extra spaces for fat and carbs? Then they will fall under calories and thus become properties of calories, which doesn't make any sense. This will result in a syntax error, which will tell you that mapping values are not allowed here because calories already have a value set, which is 105. You can either set a direct value or a hashmap. You cannot have both. So the number of spaces before each property is key in YAML. You must ensure they're in the right form to represent your data correctly. Let's take it to another level. You can have lists containing dictionaries containing lists. In this case, I have a list of fruits and the elements of the list are banana and grape, but each of these element are further dictionaries containing nutrition information. A lot of students new to YAML have reached out to me asking when to use a dictionary or a list. So let me explain this a little bit better. First of all, it is important to understand that all of what we discussed so far, such as XML, JSON or YAML are used to represent data. It could be data about an organization and all of its employees and their personal details, or it could be data about a school and all of its students and their grades. Or it could be data about an automobile manufacturing company and all of its cars and its details. It could be anything. Let's take an example of a car. A car is a single object and it has properties such as color, model, transmission, and price. To store different information or properties of a single object, we use a dictionary. In this simple dictionary, I have properties of the car defined in a key value format. This may not be as simple as this, for example, in case we need to split the model further into model name and make year, you could then represent this as a dictionary within another dictionary. In this case, the single value of model is now replaced by a small dictionary with two properties, name and year. So this is dictionary within another dictionary. Let's say we would like to store the name of six cars. The names are from by the color and the model of the car. To store this, we would use a list or an array as it is multiple items of the same type of object. Since we are only storing the names, we have a simple list of strings. What if we would like to store all information about each car, everything that we listed before such as the color, model, transmission, and price. We will then modify the array from a list of strings to a list of dictionaries. So we expand each item in the array and replace the name with the dictionary we built earlier. This way, we are able to represent all information about multiple cars in a single YAML file using a list of dictionaries. So that's the difference between dictionary, list, and list of dictionaries. I hope you understood the difference between the three and when to use each of these. Before we head over to exercises, let's take a look at some key notes. Dictionary is an unordered collection whereas lists are ordered collection. So what does that mean? The two dictionaries that you see here have the same properties for banana. However, you can see that the order of properties fat and carbs do not match. In the first dictionary, fat is defined before carbs and in the second dictionary carbs comes first followed by fat, but that doesn't really matter. The properties can be defined in any order, but the two dictionaries will still be the same as long as the values of each property match. This is not the same for lists or arrays. Arrays are ordered collection, so the order of items matter. The two lists shown are not the same because apple and banana are at different positions. This is something to keep in mind while working with data structures. Also remember, any line beginning with a hash is automatically ignored and considered as a comment. We are now ready to head over to the coding exercises and have fun playing with YAML files.