Skip to content

Instantly share code, notes, and snippets.

@monester
Created August 20, 2018 06:02
Show Gist options
  • Save monester/3f3bd87a936d1017c1f5089650b79a98 to your computer and use it in GitHub Desktop.
Save monester/3f3bd87a936d1017c1f5089650b79a98 to your computer and use it in GitHub Desktop.
# ruamel.yaml adds !!omap when using ordered dicts
# and there is no switch to avoid such behaviour
# Here is a workarond to put all keys in specific order
# and instead of following constuctions:
# list:
# - !!omap
# - [key1, value1]
# - [key2, value2]
# create this one:
# list:
# - key1: value1
# key2: value2
#
# Issue: https://bitbucket.org/ruamel/yaml/issues/226/unable-to-generate-yaml-without-omap-with
import sys
from ruamel.yaml.comments import CommentedMap as OrderedDict # to avoid '!!omap' in yaml
import ruamel.yaml
yaml = ruamel.yaml.YAML() # defaults to round-trip
yaml.version = '1.1'
data = [
OrderedDict([
('hosts', 'all'),
('vars', {'some_var': True}),
('tasks', [
OrderedDict([('name', 'print some_var'), ('debug', {'var': 'some_var'})])
]),
])
]
yaml.dump(data, sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment