Use the alias
and anchor
operators to read and write yaml aliases and anchors. The explode
operator normalises a yaml file (dereference aliases and remove anchor names).
yq
supports merge aliases (like <<: *blah
) however this is no longer in the standard yaml spec (1.2) and so yq
will automatically add the !!merge
tag to these nodes as it is effectively a custom tag.
Given a sample.yml file of:
a: &billyBob cat
then
yq eval '.a | anchor' sample.yml
will output
billyBob
Given a sample.yml file of:
a: cat
then
yq eval '.a anchor = "foobar"' sample.yml
will output
a: &foobar cat
Given a sample.yml file of:
a:b: cat
then
yq eval '.a anchor |= .b' sample.yml
will output
a: &catb: cat
Given a sample.yml file of:
b: &billyBob meowa: *billyBob
then
yq eval '.a | alias' sample.yml
will output
billyBob
Given a sample.yml file of:
b: &meow purra: cat
then
yq eval '.a alias = "meow"' sample.yml
will output
b: &meow purra: *meow
Given a sample.yml file of:
b: &meow purra:f: meow
then
yq eval '.a alias |= .f' sample.yml
will output
b: &meow purra: *meow
Given a sample.yml file of:
f:a: &a catb: *a
then
yq eval 'explode(.f)' sample.yml
will output
f:a: catb: cat
Given a sample.yml file of:
a: mike
then
yq eval 'explode(.a)' sample.yml
will output
a: mike
Given a sample.yml file of:
f:a: &a cat*a: b
then
yq eval 'explode(.f)' sample.yml
will output
f:a: catcat: b
Given a sample.yml file of:
foo: &fooa: foo_athing: foo_thingc: foo_cbar: &barb: bar_bthing: bar_thingc: bar_cfoobarList:b: foobarList_b!!merge <<:- *foo- *barc: foobarList_cfoobar:c: foobar_c!!merge <<: *foothing: foobar_thing
then
yq eval 'explode(.)' sample.yml
will output
foo:a: foo_athing: foo_thingc: foo_cbar:b: bar_bthing: bar_thingc: bar_cfoobarList:b: bar_ba: foo_athing: bar_thingc: foobarList_cfoobar:c: foo_ca: foo_athing: foobar_thing