From 5b51b560d0328e35dad5d4c77688f7577081c0ed Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 30 Nov 2022 09:25:34 -0500 Subject: [PATCH] Removed sorting to preserve original order (#74839) updated tests to reflect new order --- changelogs/fragments/unsorted.yml | 2 ++ lib/ansible/cli/inventory.py | 17 ++++++++--------- .../targets/inventory_script/inventory.json | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/unsorted.yml diff --git a/changelogs/fragments/unsorted.yml b/changelogs/fragments/unsorted.yml new file mode 100644 index 00000000000000..3910341d2bac22 --- /dev/null +++ b/changelogs/fragments/unsorted.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-inventory will not explicitly sort groups/hosts anymore, giving a chance (depending on output format) to match the order in the input sources. diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 0e8608012188d9..e8ed75e463657c 100755 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -13,7 +13,6 @@ import sys import argparse -from operator import attrgetter from ansible import constants as C from ansible import context @@ -273,11 +272,11 @@ def _graph_group(self, group, depth=0): result = [self._graph_name('@%s:' % group.name, depth)] depth = depth + 1 - for kid in sorted(group.child_groups, key=attrgetter('name')): + for kid in group.child_groups: result.extend(self._graph_group(kid, depth)) if group.name != 'all': - for host in sorted(group.hosts, key=attrgetter('name')): + for host in group.hosts: result.append(self._graph_name(host.name, depth)) if context.CLIARGS['show_vars']: result.extend(self._show_vars(self._get_host_variables(host), depth + 1)) @@ -303,9 +302,9 @@ def format_group(group): results = {} results[group.name] = {} if group.name != 'all': - results[group.name]['hosts'] = [h.name for h in sorted(group.hosts, key=attrgetter('name'))] + results[group.name]['hosts'] = [h.name for h in group.hosts] results[group.name]['children'] = [] - for subgroup in sorted(group.child_groups, key=attrgetter('name')): + for subgroup in group.child_groups: results[group.name]['children'].append(subgroup.name) if subgroup.name not in seen: results.update(format_group(subgroup)) @@ -343,14 +342,14 @@ def format_group(group): # subgroups results[group.name]['children'] = {} - for subgroup in sorted(group.child_groups, key=attrgetter('name')): + for subgroup in group.child_groups: if subgroup.name != 'all': results[group.name]['children'].update(format_group(subgroup)) # hosts for group results[group.name]['hosts'] = {} if group.name != 'all': - for h in sorted(group.hosts, key=attrgetter('name')): + for h in group.hosts: myvars = {} if h.name not in seen: # avoid defining host vars more than once seen.append(h.name) @@ -377,7 +376,7 @@ def format_group(group): results[group.name] = {} results[group.name]['children'] = [] - for subgroup in sorted(group.child_groups, key=attrgetter('name')): + for subgroup in group.child_groups: if subgroup.name == 'ungrouped' and not has_ungrouped: continue if group.name != 'all': @@ -385,7 +384,7 @@ def format_group(group): results.update(format_group(subgroup)) if group.name != 'all': - for host in sorted(group.hosts, key=attrgetter('name')): + for host in group.hosts: if host.name not in seen: seen.add(host.name) host_vars = self._get_host_variables(host=host) diff --git a/test/integration/targets/inventory_script/inventory.json b/test/integration/targets/inventory_script/inventory.json index 5046a9a83c3b5a..69ba5476866946 100644 --- a/test/integration/targets/inventory_script/inventory.json +++ b/test/integration/targets/inventory_script/inventory.json @@ -1029,9 +1029,9 @@ }, "all": { "children": [ + "ungrouped", "None", - "guests", - "ungrouped" + "guests" ] }, "guests": {