I have the following yaml file:
anchors:
kubernetes:
- kubelet: &1GiKubeReserved
kubeReserved:
cpu: 80m
memory: 1Gi
pid: 20k
- kubelet: &2GiKubeReserved
kubeReserved:
cpu: 160m
memory: 2Gi
pid: 20k
cluster:
name: test
kubernetes:
kubelet:
<<: *1GiKubeReserved
Loading the above file with ruamel.yaml removes the unused 2GiKubeReserved anchor.
- - kubelet: &2GiKubeReserved
+ - kubelet:
Snippet of the Python code:
from ruamel.yaml import YAML
yaml = YAML()
file = 'example.yaml'
with open(file, 'r+', encoding="utf-8") as f:
data = yaml.load(f)
f.seek(0)
yaml.dump(data, f)
f.truncate()
Is there a way to preserve this kind of unused anhors?
In this case I'm expecting to have no diff.
An anchor is stored on an
Anchor()instance in the.anchorattribute attached to the object. ThatAnchor()instance, apart from the anchor value, has an attributealways_dumpthat is set toFalseby default. You can set that during load time (as shown in this answer), or walk recursively over the datastructure and change thatalways_dumpattribute:which gives: