YAML is used for configuration by many technologies as it's easier and cleaner to read and write than JSON / XML.
https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
https://spacelift.io/blog/yaml
https://www.educative.io/blog/advanced-yaml-syntax-cheatsheet
Yaml better than JSON, adds:
-
comments
-
extensible data types
-
relational anchors
-
strings without quotation marks
-
mapping types preserving key order
-
must quote these constructs:
- colon followed by space or EOL
- string literal
"True"(otherwise assumes boolean) - string literal
"1.2"(otherwise assumes float)
- list_item1
- list_item2:
key1: val1
key2: val2
|
line1
line2
line3
>
single
long
lineAlways lint your YAML to catch otherwise hard to spot whitespace errors such as inconsistent indentation or tabs vs spaces.
From DevOps-Bash-tools, recursively find and lint all *.yaml / *,yml files:
check_yaml.shFrom DevOps-Python-tools, recursively find and lint all *.yaml / *,yml files:
validate_yaml.py .I run these automatically in all my GitHub repos via CI/CD.
Use & prefix anchor to mark a section and * to reference to it later in the same YAML to reduce duplication.
Prefix the *<name> reference with <<: to allow you to add more fields underneath it. Same name fields are overridden.
