YAML Basics for DevNet
YAML is a human-readable structured data format which stands for Stands for Yet Another Markup Language or now YAML Ain’t Markup Language, depending on where you look The official documentation page states the latter.
Goals of YAML
Block collections use indentation for scope and begin each entry on its new line. Block sequences use a dash and space to indicate each entry. Mappings use a colon and space to mark a key-value pair. To create a comment, you must use a hash tag.
Sequence
A sequence entry will be denoted by a dash -.
- netsw-idf-01
- netsw-idf-02
- netsw-idf-03
Scalar
A scalar is just a single value. They can be strings, numbers, Boolean, or null values.
# scalars can be quoted
“GigabitEthernet1/0/1”
# scalars can also be unquoted
GigabitEthernet1/0/2
Mapping Scalars to Sequences
Also creates an array.
routers:
- rtr-idf-01
- rtr-idf-02
switches:
- netsw-idf-01
- netsw-idf-02
Sample Inventory in YAML
—--
sites:
- name: san-jose # This is a comment. Begin block 1.
hosts:
- hostname: r1
host: 192.168.1.1
os: cisco_ios
- hostname: sw1
host: 192.168.1.5
os: cisco_ios
- hostname: sw2
host: 192.168.1.6
os: junos
- name: san-diego # This is a comment. Begin block 2.
hosts:
- hostname: r2
host: 192.168.2.1
os: cisco_ios
- hostname: sw1
host: 192.168.2.5
os: cisco_ios
Have we created a list or a dictionary? List items which is a dictionary.
Here’s a sample YAML script used to configure a VLAN on a Cisco switch.
---
- hosts: cisco
connection: network_cli
tasks:
- nxos_vlan:
vlan_id: 10
name: STORAGE
YAML is a human-readable structured data format which stands for Stands for Yet Another Markup Language or now YAML Ain’t Markup Language, depending on where you look The official documentation page states the latter.
Goals of YAML
- Data serialization standard for programming languages.
- Human-readable structured data format.
- Portable between programming languages.
- Consistent model.
- Expressive and extensible.
- Easy to implement and use.
- Writable and readable.
- Case sensitive
- Files end in .yaml or .yml
- Spaces are used instead of tabs (because various tools treat tabs differently)
- Indentation is used for structure
- Colons separate key-value pairs
- Dashes create bullet lists
- The start of a YAML file begins with three dashes
- Mappings (hashes/dictionaries)
- Sequences (arrays/lists)
- Scalars (strings/numbers)
Block collections use indentation for scope and begin each entry on its new line. Block sequences use a dash and space to indicate each entry. Mappings use a colon and space to mark a key-value pair. To create a comment, you must use a hash tag.
Sequence
A sequence entry will be denoted by a dash -.
- netsw-idf-01
- netsw-idf-02
- netsw-idf-03
Scalar
A scalar is just a single value. They can be strings, numbers, Boolean, or null values.
# scalars can be quoted
“GigabitEthernet1/0/1”
# scalars can also be unquoted
GigabitEthernet1/0/2
Mapping Scalars to Sequences
Also creates an array.
routers:
- rtr-idf-01
- rtr-idf-02
switches:
- netsw-idf-01
- netsw-idf-02
Sample Inventory in YAML
—--
sites:
- name: san-jose # This is a comment. Begin block 1.
hosts:
- hostname: r1
host: 192.168.1.1
os: cisco_ios
- hostname: sw1
host: 192.168.1.5
os: cisco_ios
- hostname: sw2
host: 192.168.1.6
os: junos
- name: san-diego # This is a comment. Begin block 2.
hosts:
- hostname: r2
host: 192.168.2.1
os: cisco_ios
- hostname: sw1
host: 192.168.2.5
os: cisco_ios
Have we created a list or a dictionary? List items which is a dictionary.
Here’s a sample YAML script used to configure a VLAN on a Cisco switch.
---
- hosts: cisco
connection: network_cli
tasks:
- nxos_vlan:
vlan_id: 10
name: STORAGE