- https://www.it-swarm.dev/ko/inheritance/yaml%EC%97%90%EC%84%9C-%EB%8D%94-%EB%B3%B5%EC%9E%A1%ED%95%9C-%EC%83%81%EC%86%8D/1069887523/
- https://yaml.org/type/merge.html
- https://mikefarah.gitbook.io/yq/v/v4.x/operators/anchor-and-alias-operators
Example
- &CENTER
x: 1
y: 2
- &LEFT
x: 0
y: 2
- &BIG
r: 10
- &SMALL
r: 1
- !!merge <<: *CENTER
r: 10
$ yq eval '.[4]' sample.yaml
!!merge <<: *CENTER
r: 10
# Merge
$ yq eval '.[4] | explode(.)' sample.yaml
x: 1
y: 2
r: 10
# Get Anchor
$ yq eval '.[0] | anchor' sample.yaml
CENTER
- &CENTER
x: 1
y: 2
- &LEFT
x: 0
y: 2
- &BIG
r: 10
- &SMALL
r: 1
- !!merge <<:
- *CENTER
- *BIG
$ yq eval '.[4]' sample.yaml
!!merge <<:
- *CENTER
- *BIG
$ yq eval '.[4] | explode(.)' sample.yaml
x: 1
y: 2
r: 10
https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-over-multiple-lines
https://mikefarah.gitbook.io/yq/
How to install
export VERSION=v4.2.0
export BINARY=yq_linux_amd64
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/bin/yq &&\
chmod +x /usr/bin/yq
yq eval --null-input '.a // "Default Value"'
$ cat <<EOF > sample.yml
a: apples
EOF
$ cat <<EOF > another.yml
b: bob
EOF
$ yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.yml
a:
b: bob
$ cat <<EOF > sample.yml
a: fieldA
b: fieldB
c: fieldC
EOF
$ yq eval '(.a, .c) |= "potatoe"' sample.yml
a: potatoe
b: fieldB
c: potatoe
$ cat <<EOF > sample.yml
a:
b: apple
c: cactus
EOF
$ yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
a:
b: frog
c: cactus
$ rm -f sample.yml && cat <<EOF > sample.yml
- candy
- apple
- sandy
EOF
$ yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
- bogs
- apple
- bogs
$ cat <<EOF > sample.yml
a:
- rad
- awesome
b:
- meh
- whatever
EOF
$ yq eval '.[] |= any_c(. == "awesome")' sample.yml
a: true
b: false
$ rm -f sample.yml && cat <<EOF > sample.yml
a:
- rad
- awesome
b:
- meh
- 12
EOF
$ yq eval '.[] |= all_c(tag == "!!str")' sample.yml
a: true
b: false
- !!map
- !!seq
- !!str
- !!int
$ cat <<EOF > sample.yml
a: cat
b: dog
c:
d:
e:
EOF
$ yq eval '.. lineComment |= .' sample.yml
a: cat # cat
b: dog # dog
c:
d:
e:
# Note the use of ... to ensure key nodes are included.
$ yq eval '... lineComment |= .' sample.yml
a: cat # cat
b: dog # dog
c: # c
d: # d
e: # e
$ cat <<EOF > sample.yml
a: cat # comment
# great
b: # key comment
EOF
$ yq eval '... comments=""' sample.yml
a: cat
b:
$ cat <<EOF > sample.yml
a:
name: frog
b:
name: blog
age: 12
EOF
$ yq eval 'del(.. | select(has("name")).name)' sample.yml
a:
b:
age: 12
$ yq eval '(.. | select(has("name")).name) = "test"' sample.yml
a:
name: test
b:
name: test
age: 12
$ cat <<EOF > sample.yml
a: cat
---
a: frog
EOF
$ yq eval '.a | ({"match": ., "doc": documentIndex})' sample.yml
match: cat
doc: 0
match: frog
doc: 1
yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml
$ cat <<EOF > sample.yml
- a:
b:
c: cat
- a:
b:
d: dog
EOF
$ yq eval '.[] | select(.a.b | has("c"))' sample.yml
a:
b:
c: cat
https://mikefarah.gitbook.io/yq/v/v4.x/operators/multiply-merge
$ cat <<EOF > sample.yml
- a:
b:
c: cat
- a:
b:
d: dog
EOF
# Merge, appending arrays
$ yq eval '.[] | select(.a.b | has("c"))' sample.yml
a:
b:
c: cat
$ rm -f sample.yml && cat <<EOF > sample.yml
a:
thing:
- 1
- 2
b:
thing:
- 3
- 4
another:
- 1
EOF
# Merge, only existing fields, appending arrays
$ yq eval '.a *?+ .b' sample.yml
thing:
- 1
- 2
- 3
- 4
$ rm -f sample.yml && cat <<EOF > sample.yml
a:
- name: fred
age: 12
- name: bob
age: 32
b:
- name: fred
age: 34
EOF
# Merge, deeply merging arrays
$ yq eval '.a *d .b' sample.yml
- name: fred
age: 34
- name: bob
age: 32