Skip to content

YAML Yaml ain't Markup Language

It is a data serialization language * Natural and meaningful * AKA human -readable * Works with agiles languages like Python, Ruby, Perl, etc
* Common Data types
* Scalars, lists, arrays * Comman Data Structure
* Indentation, dashes, colons * Common Uses
* Config files * Stroing data

Yaml Styles

Two forms of styles

  • block styles
  • flow styles

Block styles - better for humans, less compact, the YAML you've probably seen

1
2
3
4
5
6
7
host: phl-42
datacenter:
  location: Philadelphia
  cab: 13
roles:
  - web
  -dns

Flow styles - an extension of JSON, "folding" long lines of content, tags and anchors e.g.:

1
2
3
4
host: "phl-42"
datacenter: { location:
  Philadelphia , cab: 13 }
roles: [ web , dns ]

Mappings

Mappings are also known as hash maps or associative arrays and allow us to assign a value to a key that we also define.

Maps are Denoted with colon and space

my_key: my_value
In Python, the above maps to:
{'my_key': 'my_value'}
Mappings can be nested
first_level_dict_key:
  second_level_dict_key: value_in_second_level_dict

And in Python:

{
    'first_level_dict_key': {
        'second_level_dict_key': 'value_in_second_level_dict'
    }
}

Sequences (Lists)

  • Sequences are also known as arrays and lists, and allow us to provide an inventory of data using a dash-and-space combination that marks it a sequence.
  • Sequences can also be combined with mappings to create "sequences of mappings," "mappings of sequences," and even "mappings of mappings" and "sequences and sequences."

To represent lists of items, a single dash followed by a space is used. Multiple items are a part of the same list as a function of their having the same level of indentation.

- list_value_one
- list_value_two
- list_value_three

Lists can be the value of a key-value pair.

my_dictionary:
  - list_value_one
  - list_value_two
  - list_value_three
In Python, the above maps to:
{'my_dictionary': ['list_value_one', 'list_value_two', 'list_value_three']}

Scalars

It is a string or a number or a boolean. * White spaces are permitted (eg: Las Vegas) * Use single or double quotes to convert non string scalar to a string scalar
* Double quotes permits escape sequenses (eg: "\n")

Multiline Scalars

Values can span multiple lines using | or >. Spanning multiple lines using a “Literal Block Scalar” | will include the newlines and any trailing spaces.

Using a “Folded Block Scalar” > will fold newlines to spaces; it’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored. Examples are:

include_newlines: |
            exactly as you see
            will appear these three
            lines of poetry

fold_newlines: >
            this is really a
            single line of text
            despite appearances

Note

To remove the line break at the end, add - after < or | (|- or <-)

Structure

YAML can contain more than one directive (or document) in a single file. All YAML files can optionally begin with --- and end with ... This is part of the YAML format and indicates the start and end of a document.

Tags

Tags provide us with three functions:

  • The ability to assign a universal resource indicator,
  • The ability to assign local tags to that indicator
  • The ability to change how the YAML parser reads certain scalars when processing the YAML itself.

Local tags

1
They are relevent only to the exiting yaml files

Anchors

Anchors allow us to reuse data across a YAML file. Have a list or scalar that needs to be referenced again and again? With an anchor, we can use the & prefix to assign some data a name, then use the * with that name to call it repeatedly throughout the file, making updating large files a quick and easy process.

Exmaple:

---
foo: &anchor
 K1: "One"
 K2: "Two"

bar: *anchor
And in JSON
{
    "foo": {
        "K1": "One",
        "K2": "Two"
    },
    "bar": {
        "K1": "One",
        "K2": "Two"
    }
}

Anchors can also be extended using <<

---
foo: &anchor
  K1: "One"
  K2: "Two"

bar:
  <<: *anchor
  K2: "I Changed"
  K3: "Three"

And in JSON

{
    "foo": {
        "K1": "One",
        "K2": "Two"
    },
    "bar": {
        "K1": "One",
        "K2": "I Changed",
        "K3": "Three"
    }
}

The << marker means extend, so <<: *anchor means, basically, inject anchor into bar, then extend

To take only SOME keys from foo and inject them into bar? Well - now you can!

---
foo:
 <<: &anchor
   K1: "One"
 K2: "Two"

bar:
 <<: *anchor
 K3: "Three"
 ```
And in JSON
```json
{
    "foo": {
        "K1": "One",
        "K2": "Two"
    }, 
    "bar": {
        "K1": "One",
        "K3": "Three"
    }
}