title: How can I expand YAML anchors and aliases and get the resulting YAML in Bash?
body: <p>I have a <code>yaml</code> file, inside which I use anchors &amp; aliases to DRY up the file. It's a <code>docker-compose</code> file. Simple e.g. follows:</p>
  
  <pre><code>version: "3.4"
  
  x-build: &amp;build
    context: ../
  
  services:
    api:
      build:
        &lt;&lt;: *build
        dockerfile: some-path/Dockerfile
      image: gcr.io/some-project/api
  </code></pre>
  
  <p>I use <code>yq</code> and then <code>jq</code> to convert this to <code>json</code> and pass it to <code>terraform</code> so that I can re-use the information in <code>docker-compose</code> as a source of truth.</p>
  
  <p>All the aliases and anchors get removed from the resulting <code>json</code>. Not a disaster, but it would be much nicer to have it actually expanded.</p>
  
  <p>I'm looking for a command-line tool that I can run in a <code>bash</code> script to take the above <code>json</code> as an input and expand the anchors and aliases, and write the result to stdout, so something like:</p>
  
  <pre><code>version: "3.4"
  
  services:
    api:
      build:
        context: ../
        dockerfile: some-path/Dockerfile
      image: gcr.io/some-project/api
  </code></pre>
  
  <p>Does anyone know how to do this?</p>