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