@@ -38,17 +38,19 @@ def hostlist_expression(hosts):
3838 E.g. with an inventory containing:
3939
4040 [compute]
41- dev-foo-0 ansible_host=localhost
42- dev-foo-3 ansible_host=localhost
41+ dev-foo-00 ansible_host=localhost
42+ dev-foo-3 ansible_host=localhost
4343 my-random-host
44- dev-foo-4 ansible_host=localhost
45- dev-foo-5 ansible_host=localhost
46- dev-compute-0 ansible_host=localhost
47- dev-compute-1 ansible_host=localhost
44+ dev-foo-04 ansible_host=localhost
45+ dev-foo-05 ansible_host=localhost
46+ dev-compute-000 ansible_host=localhost
47+ dev-compute-001 ansible_host=localhost
4848
4949 Then "{{ groups[compute] | hostlist_expression }}" will return:
5050
51- ["dev-foo-[0,3-5]", "dev-compute-[0-1]", "my-random-host"]
51+ ['dev-foo-[00,04-05,3]', 'dev-compute-[000-001]', 'my-random-host']
52+
53+ NB: This does not guranteed to return parts in the same order as `scontrol hostlist`, but its output should return the same hosts when passed to `scontrol hostnames`.
5254 """
5355
5456 results = {}
@@ -58,19 +60,23 @@ def hostlist_expression(hosts):
5860 if m :
5961 prefix , suffix = m .groups ()
6062 r = results .setdefault (prefix , [])
61- r .append (int ( suffix ) )
63+ r .append (suffix )
6264 else :
6365 unmatchable .append (v )
6466 return ['{}[{}]' .format (k , _group_numbers (v )) for k , v in results .items ()] + unmatchable
6567
6668def _group_numbers (numbers ):
6769 units = []
68- prev = min (numbers )
69- for v in sorted (numbers ):
70+ ints = [int (n ) for n in numbers ]
71+ lengths = [len (n ) for n in numbers ]
72+ # sort numbers by int value and length:
73+ ints , lengths , numbers = zip (* sorted (zip (ints , lengths , numbers )))
74+ prev = min (ints )
75+ for i , v in enumerate (sorted (ints )):
7076 if v == prev + 1 :
71- units [- 1 ].append (v )
77+ units [- 1 ].append (numbers [ i ] )
7278 else :
73- units .append ([v ])
79+ units .append ([numbers [ i ] ])
7480 prev = v
7581 return ',' .join (['{}-{}' .format (u [0 ], u [- 1 ]) if len (u ) > 1 else str (u [0 ]) for u in units ])
7682
0 commit comments