From 4e72ce805dba1adde614631d29b31fa0952642e7 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 6 May 2020 11:05:37 -0400 Subject: [PATCH] Order my dicts (#58000) * use orderdict for yaml dictionaries * clog * SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS * allow user to toggle odict off * removed config, since requored to load config * remove unused import * Update changelogs/fragments/yaml_orderd_mappings.yml Co-authored-by: Matt Clay * Update lib/ansible/parsing/yaml/objects.py Co-authored-by: Matt Clay * Update lib/ansible/parsing/yaml/objects.py Co-authored-by: Matt Clay Co-authored-by: Matt Clay --- changelogs/fragments/yaml_orderd_mappings.yml | 2 ++ lib/ansible/parsing/yaml/objects.py | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/yaml_orderd_mappings.yml diff --git a/changelogs/fragments/yaml_orderd_mappings.yml b/changelogs/fragments/yaml_orderd_mappings.yml new file mode 100644 index 00000000000..cc118123c6f --- /dev/null +++ b/changelogs/fragments/yaml_orderd_mappings.yml @@ -0,0 +1,2 @@ +minor_changes: + - Use OrderedDict by default when importing mappings from YAML. diff --git a/lib/ansible/parsing/yaml/objects.py b/lib/ansible/parsing/yaml/objects.py index 4155994a0eb..923dc68d6e4 100644 --- a/lib/ansible/parsing/yaml/objects.py +++ b/lib/ansible/parsing/yaml/objects.py @@ -19,6 +19,7 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type +import sys import yaml from ansible.module_utils.six import text_type @@ -53,7 +54,17 @@ class AnsibleBaseYAMLObject(object): ansible_pos = property(_get_ansible_position, _set_ansible_position) -class AnsibleMapping(AnsibleBaseYAMLObject, dict): +# try to always use orderddict with yaml, after py3.6 the dict type already does this +odict = dict +if sys.version_info[:2] < (3, 7): + # if python 2.7 or py3 < 3.7 + try: + from collections import OrderedDict as odict + except ImportError: + pass + + +class AnsibleMapping(AnsibleBaseYAMLObject, odict): ''' sub class for dictionaries ''' pass