Reputation: 195
From my understanding yaml-merge is quite simple. I have two yaml files I want to merge file1.yml
& file2.yml
. With that being said the syntax should look something like yaml-merge file1.yml file2.yml
or yaml-merge *.yml
.
file1.yml
region:
- name: east
env: prod
file2.yml
region:
- name: west
env: prod
I am expecting an output of
region:
- name: east
env: prod
- name: west
env: prod
What I keep getting is a readout of file1
region:
- name: east
env: prod
What am I missing here exactly?
My goal is within my ubuntu vm merge all yml
files in the directory each all similar to file1
and file2
.
Upvotes: 0
Views: 2431
Reputation: 2210
The project yaml-merge version 0.1.1 seems quite old and unsupported to me. I suggest to use jq and yq for your task instead.
The script shows two versions how you can merge your yaml files.
Version 2 is easier to expand for merging multiple files: Simply create a stream of yaml files and pipe it into jq
.
The main work is done by jq
. yq
is justed used to convert yaml to json on the input side and json to yaml on the output side.
#!/bin/bash
FILE1='file1.yml'
FILE2='file2.yml'
echo merging yaml files - version 1
jq -n --slurpfile file1 <(yq . $FILE1) \
--slurpfile file2 <(yq . $FILE2) '
{region: ($file1[].region + $file2[].region)}' |
yq -y
echo
echo merging yaml files - version 2
echo "$(yq . $FILE1)$(yq . $FILE2)" |
jq -n 'reduce inputs as $input ([];. + $input.region) | {region: .}' | yq -y
output
merging yaml files - version 1 and 2region:
- name: east
env: prod
- name: west
env: prod
Upvotes: 2