Basic Data Types

YAML was designed to work primarily with the following data types:

  • mappings (hashes / dictionaries); 
  • sequences (arrays / lists);
  • scalars (strings / numbers).

While it can be used with most modern programming languages, it works best with languages that are built around these data structure types. This includes Python, PHP, JavaScript, and Ruby.

Scalars

Scalars are a pretty basic concept. They are the strings and numbers that make up the data on the page. There are 5 styles of scalars in YAML:

  • plain - A plain scalar does not use indicators to denote its start and end
  • single-quoted - to represent any value that does not contain special characters. No escaping occurs except that duplicate quotes '' are replaced with a single quote '
  • double-quoted - can represent any scalar value by allowing escaping with the following sequences - \u**** for Unicode character or \x** for ASCII
  • literal (block) - indicated by | where all line breaks are significant
  • folded (block) - similar to the literal style, but two consequent non-empty lines are joined to a single line separated by a space character.

EXAMPLE

Plain: 10.1.1.1

Single-quoted: '10.1.1.1'

Double-quoted: "10.1.1.1"

literal-block: |
  Example text,
  split into multiple lines,
  with line breaks

folded-block: >
  Example text,
  with blank lines:
  plus another line at the end.

Sequences

Sequences are lists with each item placed on its own line with an opening dash.

EXAMPLE: list of interfaces

- GigabitEthernet0/1
- GigabitEthernet0/2
- GigabitEthernet0/3
- GigabitEthernet0/4

Nested sequences with items and sub-items are created by placing a single space before each dash in the sub-item. 

EXAMPLE: Nested list

- 
  - GigabitEthernet0/1 
  - GigabitEthernet0/2
  - GigabitEthernet0/2
- 
  - FastEthernet1/1
  - FastEthernet1/2
  - FastEthernet1/3

More levels can be added using the same technique

EXAMPLE: Two levels deep list

- 
  - 
    - GigabitEthernet0/1
    - GigabitEthernet0/2
    - GigabitEthernet0/3

Sequences can be added to other data structure types, such as mappings or scalars.

Full Content Access is for Registered Users Only (it's FREE)...

  • Learn any CCNA, DevNet or Network Automation topic with animated explanation.
  • We focus on simplicity. Networking tutorials and examples written in simple, understandable language for beginners.