@@ -38,17 +38,17 @@ def hostlist_expression(hosts):
38
38
E.g. with an inventory containing:
39
39
40
40
[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
43
43
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
48
48
49
49
Then "{{ groups[compute] | hostlist_expression }}" will return:
50
50
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
52
"""
53
53
54
54
results = {}
@@ -57,21 +57,24 @@ def hostlist_expression(hosts):
57
57
m = pattern .match (v )
58
58
if m :
59
59
prefix , suffix = m .groups ()
60
+ print (prefix ,suffix )
60
61
r = results .setdefault (prefix , [])
61
- r .append (int ( suffix ) )
62
+ r .append (suffix )
62
63
else :
63
64
unmatchable .append (v )
64
65
return ['{}[{}]' .format (k , _group_numbers (v )) for k , v in results .items ()] + unmatchable
65
66
66
67
def _group_numbers (numbers ):
68
+ print ('numbers:' , sorted (numbers ))
67
69
units = []
68
- prev = min (numbers )
70
+ prev = min (int ( n ) for n in numbers )
69
71
for v in sorted (numbers ):
70
- if v == prev + 1 :
72
+ if int ( v ) == prev + 1 :
71
73
units [- 1 ].append (v )
72
74
else :
73
75
units .append ([v ])
74
- prev = v
76
+ print ('units:' , units )
77
+ prev = int (v )
75
78
return ',' .join (['{}-{}' .format (u [0 ], u [- 1 ]) if len (u ) > 1 else str (u [0 ]) for u in units ])
76
79
77
80
def error (condition , msg ):
0 commit comments