I have the python code below for generating (dumping) a YAML document to a file.
import yaml
import os
device_name = "NN41_R11"
ip = "10.110.11.11"
port = "2022"
def genyam():
data = {
"testbed" : {
"name" : "boot_ios"},
"devices" : {
device_name : {
"type" : "IOS",
"connections" : {
"defaults" : {
"class" : "con.con",
"a" : {
"protocol" : "telnet",
"ip" : ip,
"port" : port,
}
}
}
}
}
}
with open('/tmp/testbed.yaml', 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False)`
which generates the following YAML file
devices:
NN41_R11:
connections:
defaults:
a:
ip: 10.110.11.11
port: '2022'
protocol: telnet
class: con.con
type: IOS
testbed:
name: boot_ios
Though the key value indentation is correct it's not generating in right order. I would like to have testbed first & then devices however it's opposite now. I am suspecting it's dumping in alphabetical order. NN41_R11
is again a dictionary which contains type & connections
(type
& connections
are generated at same level but need first type:IOS
and under that connections
). Looking for ordered dump basically
The generated YAML document should be like the following:
testbed:
name: "boot-ios"
devices:
NN41_R11:
type: IOS
connections:
defaults:
class: 'con.con'
a:
protocol: telnet
ip: 10.110.11.11
port: 2022