Created
August 20, 2018 06:02
-
-
Save monester/3f3bd87a936d1017c1f5089650b79a98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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