Skip to content

Commit

Permalink
Merge pull request ansible#4351 from 2m/leading-range-fix
Browse files Browse the repository at this point in the history
Allow leading ranges in the inventory host entries.
  • Loading branch information
jctanner committed Nov 13, 2013
2 parents 2fd56ef + 7ecb5fb commit 9a7765d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
3 changes: 1 addition & 2 deletions lib/ansible/inventory/expand_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def detect_range(line = None):
Returnes True if the given line contains a pattern, else False.
'''
if (not line.startswith("[") and
line.find("[") != -1 and
if (line.find("[") != -1 and
line.find(":") != -1 and
line.find("]") != -1 and
line.index("[") < line.index(":") < line.index("]")):
Expand Down
9 changes: 5 additions & 4 deletions lib/ansible/inventory/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ def _parse_base_groups(self):
active_group_name = 'ungrouped'

for line in self.lines:
if line.startswith("["):
active_group_name = line.split(" #")[0].replace("[","").replace("]","").strip()
line = line.split("#")[0].strip()
if line.startswith("[") and line.endswith("]"):
active_group_name = line.replace("[","").replace("]","")
if line.find(":vars") != -1 or line.find(":children") != -1:
active_group_name = active_group_name.rsplit(":", 1)[0]
if active_group_name not in self.groups:
Expand All @@ -76,10 +77,10 @@ def _parse_base_groups(self):
elif active_group_name not in self.groups:
new_group = self.groups[active_group_name] = Group(name=active_group_name)
all.add_child_group(new_group)
elif line.startswith("#") or line.startswith(";") or line == '':
elif line.startswith(";") or line == '':
pass
elif active_group_name:
tokens = shlex.split(line.split(" #")[0])
tokens = shlex.split(line)
if len(tokens) == 0:
continue
hostname = tokens[0]
Expand Down
10 changes: 10 additions & 0 deletions test/TestInventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ def test_combined_range(self):
expected_hosts=['host1A','host2A','host1B','host2B']
assert sorted(hosts) == sorted(expected_hosts)

def test_leading_range(self):
i = Inventory(os.path.join(self.test_dir, 'inventory','test_leading_range'))
hosts = i.list_hosts('test')
expected_hosts=['1.host','2.host','A.host','B.host']
assert sorted(hosts) == sorted(expected_hosts)

hosts2 = i.list_hosts('test2')
expected_hosts2=['1.host','2.host','3.host']
assert sorted(hosts2) == sorted(expected_hosts2)

###################################################
### Inventory API tests

Expand Down
6 changes: 6 additions & 0 deletions test/inventory/test_leading_range
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[test]
[1:2].host
[A:B].host

[test2] # comment
[1:3].host

0 comments on commit 9a7765d

Please sign in to comment.