YAML (Ain't Markup Language), originally named as Yet Another Markup Language is a data interchange format / data serialization language which is used to store information. You may see it in .yaml or .yml format scripts.
YAML can used to store information about different things like objects, variables and lists..
Let's see an example script and identify what information is stored in it.
The script begins with --- and ends with ...
That is an option to mark the start of the script and the end.
The 1st line Router-01 is an object name. It's an object of information. It is the parent object of all the information objects under that. Rest of the code which are indented below that are the the information stored in that object in the forms of variables/ lists etc.
As an example the 2nd line name is a child object / key (variable) and is mapped to the value of WAN-RTR-A which are inside a double/single quotes.
This variable can be accessed with objectname.key format code.
As an example Router-01.name will give the value of WAN-RTR-A and Router-01.model will give the value of 2602.
routing_protocols is a list / array. It just lists some values. Note that the list items are starting with a - dash.
interfaces is also a list which is written in another format.
failover will call a null value.
model is an integer while OS_version is a floating value.
Value of serial_number variable will call 23456 only. &id_anchor is used there which we call an "anchor" so that another variable can refer it from somewhere. In this case it is referred from line 30 variable id.
features is a list of objects. There are information about 3 features, SNMP, SSH and IPSec. These 3 are the objects and they are written in 3 different ways which gives the same result.
description variable has a paragraph of information and the key starts with > which means these information will be parsed as a single line. Because of the readability of the code, it is separated by breaking lines.
summary is a variable of information which starts with | and it means the format of the paragraph is parsed just like in the code.
base is a variable list which is anchored and referred by another variable list named caller. << states that the variable along with the value is parsed when it is called.
You can also change the parsing of data types when called rather than importing the original.
As an example model: !!float 2602 will change the data type of integer to a floating number 22.0 when parsing. Also OS_version: !!str 15.4 will change the data type of floating to a string of "15.4" when parsing.
If you want to create another object named Router-02 to store information about another student like this, it should be written with correct indentation. Which means the spacing distance of the line 1 must be equal in the line of next object..
Note:-
Key-value pairs are separated by : (colon)
Lists begin with a – (hyphen)
Indentation is a requirement. You have to use spaces, you can’t use tabs
You can add comments with a #
Practicals & Notes by CCDE #20240056
No comments:
Post a Comment