When a group only has children and no hosts/vars, still return things in hash-mode, because

Ansible is not smart enough to know that 'just children' in hash mode means anything.
This commit is contained in:
Michael DeHaan 2013-07-27 17:36:02 -04:00
parent ceea3cd238
commit ac3ab82cc6
2 changed files with 15 additions and 6 deletions

View File

@ -141,11 +141,14 @@ class InventoryScriptTest(BaseScriptTest):
# Groups for this inventory should only have hosts, and no group
# variable data or parent/child relationships.
for k,v in data.items():
self.assertTrue(isinstance(v, (list, tuple)))
self.assertTrue(isinstance(v, dict))
self.assertTrue(isinstance(v['children'], (list,tuple)))
self.assertTrue(isinstance(v['hosts'], (list,tuple)))
self.assertTrue(isinstance(v['vars'], (dict)))
group = inventory.groups.get(active=True, name=k)
hosts = group.hosts.filter(active=True)
hostnames = hosts.values_list('name', flat=True)
self.assertEqual(set(v), set(hostnames))
self.assertEqual(set(v['hosts']), set(hostnames))
for group in inventory.groups.filter(active=False):
self.assertFalse(group.name in data.keys(),
'deleted group %s should not be in data' % group)
@ -187,7 +190,7 @@ class InventoryScriptTest(BaseScriptTest):
childnames = children.values_list('name', flat=True)
self.assertEqual(set(v.get('children', [])), set(childnames))
else:
self.assertFalse('children' in v)
self.assertTrue(len(v['children']) == 0)
def test_valid_host(self):
# Host without variable data.
@ -268,4 +271,4 @@ class InventoryScriptTest(BaseScriptTest):
rc, stdout, stderr = self.run_inventory_script(list=True, host='blah')
self.assertNotEqual(rc, 0, stderr)
self.assertEqual(json.loads(stdout), {})

View File

@ -564,8 +564,14 @@ class InventoryScriptView(RetrieveAPIView):
'children': list(children.values_list('name', flat=True)),
'vars': group.variables_dict,
}
group_info = dict(filter(lambda x: bool(x[1]),
group_info.items()))
# this confuses the inventory script if the group
# has children set and no variables or hosts.
# no other reason to do this right?
#
# group_info = dict(filter(lambda x: bool(x[1]),
# group_info.items()))
if group_info.keys() in ([], ['hosts']):
data[group.name] = group_info.get('hosts', [])
else: