Some tricks of YAML
In this post I will talk about not very well-known features of the YAML language.
System administration has changed a bit over the past few years. Instead of small bash scripts, we now have huge configuration system projects. Puppet with a million modules is ready to "configure" any machine for us, put everything in and configure everything. And of course, this is the crowning achievement of Hiera automation - the control system of the control system.
In the beginning, the idea of separating all configuration data into a hierarchical structure and editing beautiful and convenient YAML files seems incredibly seductive, especially if you recall the many formats of config files, the creators of which seem to have participated in competitions for original thinking. However, very soon we find ourselves with thousands of YAML lines. Let's see how you can use YAML to make our configurations easier to read and maintain.
Very often you need to cram multiline text into hiera. There are at least 3 ways to do this.
Please, now that you have learned how to make multi-line text, do not use the first method.
Sometimes you need to cram a lot of sub-lines into one line. In YAML, and this can be done in at least three ways.
Interestingly, all 3 methods can be used anywhere, for example, in lists:
YAML since version 1.2 is a superset of JSON. That is, everything that is correct for JSON is also suitable for YAML. This can sometimes be used to improve readability.
An obvious drawback of the latter design is that when changing one parameter, the entire line changes, and alignment can also ruin the history in GIT. However, readability is worth it.
Another use case for JSON-style is matrix definition.
They did not expect? I was also surprised. It turns out that there is also inheritance in YAML.
Or, if you rewrite it even shorter using JSON, you can get a pretty plate:
In the previous example, we looked at inheritance, and you probably noticed the elements & and *. These elements allow you to define a link to the element and then use it.
Well, and finally, a one-liner for checking YAML files:
YAML 1.2 format specification
Yaml Cookbook for Ruby
And may KISS and DRY be with you .
UPD
I will be glad to see your examples of interesting use of YAML in the comments.
Prologue
System administration has changed a bit over the past few years. Instead of small bash scripts, we now have huge configuration system projects. Puppet with a million modules is ready to "configure" any machine for us, put everything in and configure everything. And of course, this is the crowning achievement of Hiera automation - the control system of the control system.
In the beginning, the idea of separating all configuration data into a hierarchical structure and editing beautiful and convenient YAML files seems incredibly seductive, especially if you recall the many formats of config files, the creators of which seem to have participated in competitions for original thinking. However, very soon we find ourselves with thousands of YAML lines. Let's see how you can use YAML to make our configurations easier to read and maintain.
Examples
Multiline text
Very often you need to cram multiline text into hiera. There are at least 3 ways to do this.
multiliners:
ugly_multiline: "ugly\nugly\nugly\nugly\n"
multiline_with_line_ending: |
multiline text
with ending
multiline_without_line_ending: |-
multiline text
without ending
Please, now that you have learned how to make multi-line text, do not use the first method.
Single line text
Sometimes you need to cram a lot of sub-lines into one line. In YAML, and this can be done in at least three ways.
singleliners:
simple:
single
line
text
single-line-text: >-
single
line
text
single-line-text-with-line-ending: >
single
line
text
Interestingly, all 3 methods can be used anywhere, for example, in lists:
commands:
- do something with --a long --list of --parameters
- do something
with
--a long
--list of
--parameters
JSON-style
YAML since version 1.2 is a superset of JSON. That is, everything that is correct for JSON is also suitable for YAML. This can sometimes be used to improve readability.
json:
vm-profiles-yaml:
small:
cpu: 2
ram: 2
disk: 10
os: rhel6
large:
cpu: 4
ram: 4
disk: 10
os: rhel6
vm-profiles-json:
small: { cpu: 2, ram: 2, disk: 10, os: rhel6 }
large: { cpu: 4, ram: 4, disk: 10, os: rhel6 }
An obvious drawback of the latter design is that when changing one parameter, the entire line changes, and alignment can also ruin the history in GIT. However, readability is worth it.
Matrices
Another use case for JSON-style is matrix definition.
matrices:
matrix_json_style: [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
]
matrix_yaml_style:
- [1, 0, 0]
- [0, 1, 0]
- [0, 0, 1]
Inheritance
They did not expect? I was also surprised. It turns out that there is also inheritance in YAML.
inheritance:
_basic: &basic
cpu: 2
ram: 2
disk: 10
os: rhel6
vm-profiles:
small:
<<: *basic
cpu: 1
large:
<<: *basic
cpu: 4
Or, if you rewrite it even shorter using JSON, you can get a pretty plate:
inheritance:
_basic: &basic
cpu: 2
ram: 2
disk: 10
os: rhel6
vm-profiles:
small: {<<: *basic, cpu: 1}
large: {<<: *basic, cpu: 4}
References
In the previous example, we looked at inheritance, and you probably noticed the elements & and *. These elements allow you to define a link to the element and then use it.
references:
value1: &reference "Don't repeat yourself!"
value2: *reference
Check
Well, and finally, a one-liner for checking YAML files:
# requires PyYAML
alias yaml2json='python -c "import sys,yaml,json;sys.tracebacklimit=0;print(json.dumps(yaml.load(open(sys.argv[1]).read()), indent=2))"'
Read
YAML 1.2 format specification
Yaml Cookbook for Ruby
And may KISS and DRY be with you .
UPD
I will be glad to see your examples of interesting use of YAML in the comments.