YamlExtras provides a set of utilities to marginally expand the YAML file format.
When dealing with large configuration files, occasionally it can be useful to break the file up into multiple pieces. Take for example the following yaml:
--- name: John date_of_birth: year: 1986 month: 1 day: 30 location: planet: Earth country: USA state: Michigan
Using the file inclusion support in YamlExtras, we can break this up into a few different files:
--- name: John date_of_birth: INCLUDE: dob.yml location: INCLUDE: location.yml
--- year: 1986 month: 1 day: 30
--- planet: Earth country: USA state: Michigan
We can produce a merged YAML file on stdout with the following:
require 'yaml_extras' puts YamlExtras.new("main.yml").result.to_yaml
This snippet produces the original document seen here:
--- name: John date_of_birth: year: 1986 month: 1 day: 30 location: planet: Earth country: USA state: Michigan
There are a few things to keep in mind:
-
Cyclic inclues are not allowed and will cause an exception.
-
Includes cannot be referenced from an Array.
-
All YAML files are evaluated independently and merged based on the Ruby structure.
-
Built-in YAML features like references and merging do not impact more than the file in which they appear.