Understanding YAML
In this section, we will learn the different ways in which the YAML data is represented.
Last updated
In this section, we will learn the different ways in which the YAML data is represented.
Last updated
Whenever you want to send some data structure or an object across computer networks, say the Internet, you have to turn it into a special format to read it and store it. The process is commonly known as serialization and is of enormous importance on the web. A common usage example of serialization is when reading data from databases and transferring it across the web.
YAML is a data serialization format that stands for YAML ain’t Markup language.
The main advantage of using YAML is readability and writability. If you have a configuration file that needs to be easier for humans to read, it’s better to use YAML. YAML is not a complete substitution of JSON as JSON and XML have their places too; nevertheless, it’s useful learning YAML.
Another benefit of YAML is its support of various data types like cases, arrays, dictionaries, lists, and scalars. It has good support for the most popular languages like JavaScript, Python, Ruby, Java, etc.
Rules for Creating YAML file
When you are creating a file in YAML, you should remember the following basic rules:
YAML is case sensitive
The files should have .yaml or .yml as the extension
YAML does not allow the use of tabs while creating YAML files; spaces are allowed instead
The file starts with three dashes. These dashes indicate the start of a new YAML document.
The following are the building blocks of a YAML file:
Key Value Pair — The basic type of entry in a YAML file is of a key value pair. After the Key and colon there is a space and then the value.
Arrays/Lists — Lists would have a number of items listed under the name of the list. The elements of the list would start with a -
. There can be a n
of lists, however the indentation of various elements of the array matters a lot.
Dictionary/Map — A more complex type of YAML file would be a Dictionary and Map.
Consider the following diagram, which has details about “Banana.” There are 3 attributes:
Calories = 200
Fat = 0.5g
Carbs = 30g
Suppose there is an extra indentation or spaces used — then the whole meaning of the YAML object changes as you can see below. So we need to be careful with respect to indentation and spaces.
Here are a few notes about YAML syntax:
Dictionary is an unordered collection whereas lists are ordered collection.
We can use all of the above types to create an object with nested values, such as lists of dictionaries, dictionaries whose values are lists or a mix of both:
Also remember any line beginning with a hash is automatically ignored and considered as a comment.
.
.
.
https://www.tutorialspoint.com/ansible/ansible_yaml_basics.htm
https://www.guru99.com/ansible-tutorial.html
https://www.tutorialspoint.com/yaml/yaml_introduction.htm
https://www.tutorialspoint.com/yaml/yaml_basics.htm
https://geekflare.com/yaml-introduction/
https://developer.ibm.com/technologies/containers/tutorials/yaml-basics-and-usage-in-kubernetes/
https://dev.to/paulasantamaria/introduction-to-yaml-125f
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
.