I have the following yaml file and want to replace MoreVariables: "placeholder"
with a dynamic amount of key, value pairs in the same style as var1, var2. I also want replace Something 2
with different variables without knowing in advance how many variables I may want to append. Is this doable?
Object1:
var1: sun
var2: moon
MoreVariables: placeholder
Object2:
Arguments:
Something: a
Something: placeholder2
OtherStuff: hello
I’m reading my input from another yaml file:
Object1:
MoreVariables:
var3: sky
var4: blue
Object2:
Something:
var1: flower
var2: tree
This is the output I’m expecting:
Object1:
var1: sun
var2: moon
var3: sky
var4: blue
Object2:
Arguments:
Something1: a
var1: flower
var2: tree
OtherStuff: hello
I can read both yaml files as string, but because I don’t know how many arguments I have in variables.yaml, I don’t know how to replace the placeholders effectively while maintaining yaml format.
from cfn_tools import dump_yaml, load_yaml
with open(“my_file1”, “r”) as f:
data = dump_yaml(load_yaml(f.read()))