Skip to content

Commit

Permalink
Removed sorting to preserve original order (ansible#74839)
Browse files Browse the repository at this point in the history
updated tests to reflect new order
  • Loading branch information
bcoca authored Nov 30, 2022
1 parent 1998521 commit 5b51b56
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/unsorted.yml
Original file line number Diff line number Diff line change
@@ -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.
17 changes: 8 additions & 9 deletions lib/ansible/cli/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import sys

import argparse
from operator import attrgetter

from ansible import constants as C
from ansible import context
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -377,15 +376,15 @@ 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':
results[group.name]['children'].append(subgroup.name)
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)
Expand Down
4 changes: 2 additions & 2 deletions test/integration/targets/inventory_script/inventory.json
Original file line number Diff line number Diff line change
Expand Up @@ -1029,9 +1029,9 @@
},
"all": {
"children": [
"ungrouped",
"None",
"guests",
"ungrouped"
"guests"
]
},
"guests": {
Expand Down

0 comments on commit 5b51b56

Please sign in to comment.