Allow inventory scripts to define groups of groups and group vars

This commit is contained in:
Daniel Hokka Zakrisson 2012-11-27 00:13:56 +01:00
commit 8d309e0fa3
3 changed files with 43 additions and 20 deletions

View file

@ -278,6 +278,7 @@ class Inventory(object):
if updated is not None:
vars.update(updated)
vars.update(host.get_variables())
if self._is_script:
cmd = [self.host_list,"--host",hostname]
try:
@ -287,14 +288,7 @@ class Inventory(object):
(out, err) = sp.communicate()
results = utils.parse_json(out)
# FIXME: this is a bit redundant with host.py and should share code
results['inventory_hostname'] = hostname
results['inventory_hostname_short'] = hostname.split('.')[0]
groups = [ g.name for g in host.get_groups() if g.name != 'all' ]
results['group_names'] = sorted(groups)
vars.update(results)
else:
vars.update(host.get_variables())
return vars
def add_group(self, group):

View file

@ -45,15 +45,25 @@ class InventoryScript(object):
all=Group('all')
groups = dict(all=all)
group = None
for (group_name, hosts) in self.raw.items():
for (group_name, data) in self.raw.items():
group = groups[group_name] = Group(group_name)
host = None
for hostname in hosts:
if not hostname in all_hosts:
all_hosts[hostname] = Host(hostname)
host = all_hosts[hostname]
group.add_host(host)
# FIXME: hack shouldn't be needed
all.add_host(host)
if not isinstance(data, dict):
data = {'hosts': data}
if 'hosts' in data:
for hostname in data['hosts']:
if not hostname in all_hosts:
all_hosts[hostname] = Host(hostname)
host = all_hosts[hostname]
group.add_host(host)
if 'vars' in data:
for k, v in data['vars'].iteritems():
group.set_variable(k, v)
all.add_child_group(group)
# Separate loop to ensure all groups are defined
for (group_name, data) in self.raw.items():
if isinstance(data, dict) and 'children' in data:
for child_name in data['children']:
if child_name in groups:
groups[group_name].add_child_group(groups[child_name])
return groups