📜 ⬆️ ⬇️

Some YAML Tricks

In this post I will talk about not very well-known features of the language YAML.

Prologue


System administration over the past few years has changed somewhat. Instead of small bash scripts, we now have huge configuration system projects. Puppet with a million modules is ready to “configure” any type of machine for us, install everything and configure everything. And of course, this celebration is crowned by the automation of Hiera - the control system of the control system.

At the beginning, the idea of ​​isolating all configuration data into a hierarchical structure and editing beautiful and convenient YAML files seems incredibly seductive, especially if we recall the many formats of config files whose creators seem to have participated in original thinking competitions. However, very soon we find ourselves with thousands of lines of YAML. 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 in multi-line hiera text. For this there are at least 3 ways.

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, 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. Sometimes this can 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 } 

The obvious disadvantage of the last construction is that changing one parameter changes the entire line, and the alignment can also spoil the story in GIT. However, readability is worth it.

Matrices

Another use of JSON-style is the definition of matrices.

 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 surprised too. 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 beautiful label:

 inheritance: _basic: &basic cpu: 2 ram: 2 disk: 10 os: rhel6 vm-profiles: small: {<<: *basic, cpu: 1} large: {<<: *basic, cpu: 4} 

Links

In the previous example, we looked at inheritance, and you probably noticed the elements & and *. These elements allow you to define a link to an element and then use it.

 references: value1: &reference "Don't repeat yourself!" value2: *reference 

Check


Well, and finally, 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 would be glad to see in the comments your examples of interesting use of YAML.

Source: https://habr.com/ru/post/270097/


All Articles