YAML, or YAML Ain’t Markup Language, is a human-readable data serialisation language just like XML and JSON, it is often used to write configuration files. YMAL files have two extensions .yaml
or .yml
.
YAML data structure is defined using line separation and indentation, all YAML files can optionally start with ---
and end with ...
.
---
name: John Doe # no quotation string
age: 30
address:
street: "123 Main St" # double quotation string
city: Anytown
state: CA
phone_numbers:
- home: 555-555-5555
- work: 555-555-1234
...
Data types
String String can be in double quotation marks (” ”), single quotation marks (’ ’), or no quotation marks
Boolean
YAML indicates boolean values with the keywords True
, On
and Yes
for true. False is indicated with False
, Off
, or No
.
foo: True
bar: Yes
light: On
Dictionary
A dictionary is represented in a key value pair form (key: value
), the colon must be followed by a space.
# an employee record
martin:
name: Martin D'vloper
job: Developer
skill: Elite
List
List contains multiple values, all members of a list are lines at the same indentation level, starting with a dash (-
) and followed by a space.
# multiple employee records
- martin:
name: Martin D'vloper
job: Developer
skills:
- python
- perl
- pascal
- tabitha:
name: Tabitha Bitumen
job: Developer
skills:
- lisp
- fortran
- erlang
Multiple lines value
Values can span multiple lines using |
or >
. “Literal Block Scalar” |
will include the newlines and any trailing spaces, preserve the exact value. Using a “Folded Block Scalar” >
will fold newlines to spaces;
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
start a new line
The value of include_newlines
and fold_newlines
would be:
exactly as you see
will appear these three
lines of poetry
this is really a single line of text despite appearances
start a new line
Additional indicators
|-
or>-
: Strip the final newline.|+
or>+
: Preserve the final newline.|
or>
: (without any additional indicators) keeps the final newline.
literal: |-
This text will not have a trailing newline.
folded: >+
This text will preserve the final newline (\n).
Back to parent page: DevOps