Appending yaml anchors in helm charts

6/6/2018

I'm building a helm chart and got a problem with my values.yaml file. Since yaml doesn't support variables inside itself I tried anchors but although something like this works:

foo: &anchor A
bar: *anchor

with output

foo: A
bar: A

I need the anchor to be appended to some string, like

foo: &anchor A
baz: &anotherAnchor B
bar: www.*anchor.*anotherAnchor.com

with output

foo: A
baz: B
bar: www.A.B.com

Is it even possible to do that?

-- FRC
kubernetes-helm
templates
yaml

1 Answer

6/7/2018

Not with YAML as it exists today. YAML does have no features for transforming data. People tend to use templating engines like Jinja to do stuff like this as a preprocessing step (see Ansible, SaltStack, others).

Note that while the anchor/alias feature seems to be frequently used for data deduplication, that was never the intention. They are, originally, meant to be used to serialize in-memory data structures that may contain cycles, or anything else where multiple variables point to the same object. In YAML semantics, your first two code snippets are not identical, because the first one defines one string object A which is referred to at two locations, while the second one defines two string objects A. However, for configuration data, this typically does not make a difference (unless the configuration is modified in-memory and written back).

This is why YAML lacks any kind of transformation features that would allow operations like the one you want to do.

-- flyx
Source: StackOverflow