Skip to content

Instantly share code, notes, and snippets.

@kholis
Last active August 29, 2015 14:16
Show Gist options
  • Save kholis/d603feb9ff1a67a427c6 to your computer and use it in GitHub Desktop.
Save kholis/d603feb9ff1a67a427c6 to your computer and use it in GitHub Desktop.
Fabric hosts file parsing
import re
hosts_list = []
with open('hosts_file') as f:
for line in f:
if not (line.strip()=='' or line.startswith('#')):
if "[" in line:
prefix = line.split('[')[0]
m = re.search(r"\[([0-9\-]+)\]", line)
a = m.group(1)
start, end = a.split('-')
start_i = int(start)
end_i = int(end) + 1
if start.startswith('0'):
for i in range(start_i, end_i):
val = prefix + "%02d" % i
hosts_list.append(val)
else:
for i in range(start_i, end_i):
val = prefix + "%d" % i
hosts_list.append(val)
else:
hosts_list.append(line)
env.hosts = hosts_list
@kholis
Copy link
Author

kholis commented Apr 6, 2015

Allow blank space in hosts_file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment