Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I expand YAML anchors and aliases and get the resulting YAML in Bash?

Tags:

bash

yaml

I have a yaml file, inside which I use anchors & aliases to DRY up the file. It's a docker-compose file. Simple e.g. follows:

version: "3.4"

x-build: &build
  context: ../

services:
  api:
    build:
      <<: *build
      dockerfile: some-path/Dockerfile
    image: gcr.io/some-project/api

I use yq and then jq to convert this to json and pass it to terraform so that I can re-use the information in docker-compose as a source of truth.

All the aliases and anchors get removed from the resulting json. Not a disaster, but it would be much nicer to have it actually expanded.

I'm looking for a command-line tool that I can run in a bash script to take the above json as an input and expand the anchors and aliases, and write the result to stdout, so something like:

version: "3.4"

services:
  api:
    build:
      context: ../
      dockerfile: some-path/Dockerfile
    image: gcr.io/some-project/api

Does anyone know how to do this?

like image 989
Sebastian Nemeth Avatar asked Oct 16 '22 14:10

Sebastian Nemeth


1 Answers

Why aren't you taking the yaml as source of truth? You should have a look on spruce. Basically it is a yaml/json merge tool that can also convert to json. However, it also throws your anchors and aliases away.

Convert yml to json, thus no need for yq and jq action anymore :)

spruce json

Merge yml files:

spruce merge file1.yml file2.yml

Your example:

$ spruce merge spruce-input.yml
services:
  api:
    build:
      context: ../
      dockerfile: some-path/Dockerfile
    image: gcr.io/some-project/api
version: "3.4"
x-build:
  context: ../

$ spruce json spruce-input.yml | jq -r
{
  "services": {
    "api": {
      "build": {
        "context": "../",
        "dockerfile": "some-path/Dockerfile"
      },
      "image": "gcr.io/some-project/api"
    }
  },
  "version": "3.4",
  "x-build": {
    "context": "../"
  }
}
like image 196
muehsi Avatar answered Nov 15 '22 08:11

muehsi