Merge pull request #1283 from jhoekx/fix-inventory-api-groups

Fix inventory API groups
This commit is contained in:
Michael DeHaan 2012-10-10 09:51:36 -07:00
commit ae94b050af
3 changed files with 19 additions and 4 deletions

View file

@ -35,6 +35,7 @@ class InventoryScript(object):
self.groups = self._parse() self.groups = self._parse()
def _parse(self): def _parse(self):
all_hosts = {}
groups = {} groups = {}
self.raw = utils.parse_json(self.data) self.raw = utils.parse_json(self.data)
@ -45,7 +46,9 @@ class InventoryScript(object):
group = groups[group_name] = Group(group_name) group = groups[group_name] = Group(group_name)
host = None host = None
for hostname in hosts: for hostname in hosts:
host = Host(hostname) if not hostname in all_hosts:
all_hosts[hostname] = Host(hostname)
host = all_hosts[hostname]
group.add_host(host) group.add_host(host)
# FIXME: hack shouldn't be needed # FIXME: hack shouldn't be needed
all.add_host(host) all.add_host(host)

View file

@ -246,3 +246,13 @@ class TestInventory(unittest.TestCase):
actual_hosts = inventory.get_hosts(host_names) actual_hosts = inventory.get_hosts(host_names)
actual_host_names = [host.name for host in actual_hosts] actual_host_names = [host.name for host in actual_hosts]
assert host_names == actual_host_names assert host_names == actual_host_names
def test_script_multiple_groups(self):
inventory = self.script_inventory()
vars = inventory.get_variables('zeus')
print "VARS=%s" % vars
assert vars == {'inventory_hostname': 'zeus',
'inventory_hostname_short': 'zeus',
'group_names': ['greek', 'major-god']}

View file

@ -13,15 +13,17 @@ parser.add_option('-e', '--extra-vars', default=None, dest="extra")
options, args = parser.parse_args() options, args = parser.parse_args()
systems = { systems = {
"ungouped": [ "jupiter", "saturn" ], "ungrouped": [ "jupiter", "saturn" ],
"greek": [ "zeus", "hera", "poseidon" ], "greek": [ "zeus", "hera", "poseidon" ],
"norse": [ "thor", "odin", "loki" ] "norse": [ "thor", "odin", "loki" ],
"major-god": [ "zeus", "odin" ],
} }
variables = { variables = {
"thor": { "thor": {
"hammer": True "hammer": True
} },
"zeus": {},
} }
if options.list_hosts == True: if options.list_hosts == True: