Skip to content

Instantly share code, notes, and snippets.

@bgalvao
Last active March 31, 2022 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgalvao/2c7992fac9b5dd5641f733ecbd2c2861 to your computer and use it in GitHub Desktop.
Save bgalvao/2c7992fac9b5dd5641f733ecbd2c2861 to your computer and use it in GitHub Desktop.
Docker Compose/Stack templating and file generation using jsonnet and yq

Prereqs: install jsonnet, yaml2jsonnet (optional) and yq:

If you already have .yaml files lying around, or prefer to start with them, you don't have to start from scratch: convert them to jsonnet using yaml2jsonnet

yaml2jsonnet ../config/authelia/configuration.yaml | jsonnetfmt - -o config.authelia.jsonnet

Let that be your starting point. Now edit to your will. If you want to test it (see what it renders) or save it, then:

# preview in the console
jsonnet ./config.authelia.jsonnet | yq -y

# save its yaml render
jsonnet ./config.authelia.jsonnet | yq -y > config.authelia.yaml

Let's say we want to only render some fields in presence of some env vars - but it can be any other condition, such as flag of your choice.

{
  log: {
    [if false then 'file_path' else null]: '/data/authelia.log',
    format: 'json',
    keep_stdout: true,
    level: 'info',  //
  },
}

This won't render the file_path field:

log:
  # where is the file_path?
  format: json
  keep_stdout: true
  level: info

Credits to jjo.


Extending the above idea, you can make some sub-bodies of your configuration completely optional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment