Hierarchical Data
Choria Hierarchical Data Resolver is a small data resolver inspired by Hiera. It evaluates a YAML or JSON document alongside a set of facts to produce a final data map. The resolver supports first and deep merge strategies and relwies on simple string interpolation for hierarchy entries.
It is optimized for single files that hold the hierarchy and data rather than the multi-file approach common in Hiera.
Major features:
- Lookup expressions based on a full language
- Types are supported, and lookups can return typed data
- Command line tool that includes built-in system facts
- Go library
Usage
Here’s an annotated example:
The templating used here is identical to that used in the Data documentation section except that only the lookup() function was added not ones for accessing files etc.
CLI Example
A small utility is provided to resolve a hierarchy file and a set of facts. This utility has some different rules and behaviors for loading facts than the ccm suite in general since we want it to be a genericly usable tool even without system facts etc, so we make it easy to pass flags on the CLI etc.
Given the input file data.json:
We can run the utility like this:
It can also produce YAML output:
It can also produce Environment Variable output:
In these examples we provided facts from a file or on the CLI, we can also populate the facts from an internal fact provider, first we view the internal facts:
Now we resolve the data using those facts:
We can also populate the environment variables as facts, variables will be split on the = and the variable name becomes a fact name.
These facts will be merged with ones from the command line and external files and all can be combined
Data in NATS
NATS is a lightweight messaging system that is straightforward to run and host; it supports being used as a Key-Value store.
We can’t cover NATS here in detail here, but hierarchy data can be stored in NATS Key-Value stores and used in the ccm ensure and ccm hiera commands.
To use NATS as a hierarchy store, you need to configure a NATS context - a way to configure authentication or URLs for NATS.
Let’s add a context called ccm for our needs:
We create a KV store that is stored replicated in a cluster and store the hierarchy from hiera.yaml in a Key called data:
We can now parse the hierarchy using system facts, this is identical to using the file locally:
Go example
Supply a YAML document and a map of facts. The resolver will parse the hierarchy, replace {{ lookup('facts.fact') }} placeholders, and merge the matching sections.
Here the hierarchy key defines the lookup strategies and the data key defines what will be returned.
The rest is the hierarchy data.