YAML Introduction
YAML (YAML Ain't Markup Language) is a human-readable data serialization standard that is commonly used for configuration files and data exchange between languages with different data structures. It will be extremely useful in your network automation projects. If you are not familiar with YAML, there are many excellent tutorials and resources available.
The following exercises provide a basic introduction to YAML. To verify your solution, you can use online converters or CLI tools like yq
or Nettowel. When working with YAML keep in mind the difference between version 1.1 and 1.2! yq
and nettowel
use version 1.2 as default.
JSON to YAML
- Convert the following JSON to YAML:
json
{
"host": "Switch01",
"ip": "10.11.12.13",
"dns": "8.8.8.8",
"domain_name": "network.automation.lab",
"fqdn": "Switch01.network.automation.lab"
}
Solution
- Convert the following JSON to YAML:
- Convert the following JSON to YAML:
json
{
"OSPF": {
"area": 10,
"hello": 5,
"interface-type": "p2p"
},
"bgp": {
"asn": 65123,
"rr": true
}
}
- Convert the following JSON to YAML:
- Convert the following JSON to YAML:
json
{
"ospf": {
"area": 0,
"interfaces": [
{
"name": "Gig1/0/1",
"passive": false,
"type": "p2p"
},
{
"name": "Gig1/0/1",
"passive": true,
"type": "broadcast"
}
],
"redistribute": [
"bgp",
"isis"
],
"id": "10.10.10.10"
}
}
Solution
Craft YAML
- Create a sequence of mappings for vlan definitions with
id
andname
as keys.
Solution
- Create a mapping for interfaces with tagged and untagged vlan sequences.
Solution
- Write a YAML file with
hostname
,domain-name
, a list ofdns
servers, a list ofntp
servers and a list ofsyslog
servers.
Solution
YAML to JSON
- Convert the following YAML to JSON:
yaml
%YAML 1.1
---
sexagesimal: 123:10:10
port: 80
alt_port: !!str 8080
octal: 02472256
hexadecimal: 0x_0A_74_AE
binary: 0b1010_0111_0100_1010_1110
...
Solution
- Convert the following YAML to JSON:
yaml
%YAML 1.2
---
sexagesimal: 123:10:10
port: 80
alt_port: !!str 8080
octal: 02472256
hexadecimal: 0x_0A_74_AE
binary: 0b1010_0111_0100_1010_1110
...
Solution
- Convert the following YAML to JSON:
- Convert the following YAML to JSON:
Tip
Always use True|true
or False|false
for booleans.
- Convert the following YAML to JSON:
yaml
---
name: &a Network Automation Labs
alias: *a
mgmt_vlan: &mgmt_vlan
name: mgmt
id: 4
access_vlan: &access_vlan
name: access
id: 123
vlans:
- *mgmt_vlan
- *access_vlan
...
Solution
- Convert the following YAML to JSON: