diff --git a/lib/ansible/inventory/yaml.py b/lib/ansible/inventory/yaml.py index 0c2853ad944..9b950e21d64 100644 --- a/lib/ansible/inventory/yaml.py +++ b/lib/ansible/inventory/yaml.py @@ -106,6 +106,7 @@ class InventoryParserYaml(object): elif type(item) == dict and 'host' in item: host = self._make_host(item['host']) + vars = item.get('vars', {}) if type(vars)==list: varlist, vars = vars, {} @@ -113,5 +114,23 @@ class InventoryParserYaml(object): vars.update(subitem) for (k,v) in vars.items(): host.set_variable(k,v) + + groups = item.get('groups', {}) + if type(groups) in [ str, unicode ]: + groups = [ groups ] + if type(groups)==list: + for subitem in groups: + if subitem in self.groups: + group = self.groups[subitem] + else: + group = Group(subitem) + self.groups[group.name] = group + all.add_child_group(group) + group.add_host(host) + grouped_hosts.append(host) + if host not in grouped_hosts: ungrouped.add_host(host) + + # make sure ungrouped.hosts is the complement of grouped_hosts + ungrouped_hosts = [host for host in ungrouped.hosts if host not in grouped_hosts] diff --git a/test/TestInventory.py b/test/TestInventory.py index ef551e5816a..bc1cbde0311 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -221,15 +221,14 @@ class TestInventory(unittest.TestCase): def test_yaml(self): inventory = self.yaml_inventory() hosts = inventory.list_hosts() - print hosts - expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus'] self.compare(hosts, expected_hosts) def test_yaml_all(self): inventory = self.yaml_inventory() hosts = inventory.list_hosts('all') - expected_hosts=['jupiter', 'saturn', 'mars', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki'] + expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus'] self.compare(hosts, expected_hosts) def test_yaml_norse(self): @@ -323,3 +322,29 @@ class TestInventory(unittest.TestCase): assert 'group_names' in vars assert sorted(vars['group_names']) == [ 'norse', 'ruler' ] + def test_yaml_some_animals(self): + inventory = self.yaml_inventory() + hosts = inventory.list_hosts("cat:mouse") + expected_hosts=['garfield', 'jerry', 'mickey', 'tom'] + self.compare(hosts, expected_hosts) + + def test_yaml_comic(self): + inventory = self.yaml_inventory() + hosts = inventory.list_hosts("comic") + expected_hosts=['garfield', 'odie'] + self.compare(hosts, expected_hosts) + + def test_yaml_orange(self): + inventory = self.yaml_inventory() + hosts = inventory.list_hosts("orange") + expected_hosts=['garfield', 'goofy'] + self.compare(hosts, expected_hosts) + + def test_yaml_garfield_vars(self): + inventory = self.yaml_inventory() + vars = inventory.get_variables('garfield') + assert vars == {'ears': 'pointy', + 'inventory_hostname': 'garfield', + 'group_names': ['cat', 'comic', 'orange'], + 'nose': 'pink'} + diff --git a/test/yaml_hosts b/test/yaml_hosts index cf73e8d4537..adfcf5bce51 100644 --- a/test/yaml_hosts +++ b/test/yaml_hosts @@ -1,5 +1,7 @@ --- +# Below is the original way of defining hosts and groups. + - jupiter - host: saturn vars: @@ -37,3 +39,44 @@ - group: multiple hosts: - saturn + +# Here we demonstrate that groups can be defined on a per-host basis. +# When managing a large set of systems this format makes it easier to +# ensure each of the systems is defined in a set of groups, compared +# to the standard group definitions, where a host may need to be added +# to multiple disconnected groups. + +- host: garfield + groups: [ comic, cat, orange ] + vars: + - nose: pink + +- host: odie + groups: [ comic, dog, yellow ] + +- host: mickey + groups: [ cartoon, mouse, red ] + +- host: goofy + groups: [ cartoon, dog, orange ] + +- host: tom + groups: [ cartoon, cat, gray ] + +- host: jerry + groups: [ cartoon, mouse, brown ] + +- group: cat + vars: + - ears: pointy + - nose: black + +- group: dog + vars: + - ears: flappy + - nose: black + +- group: mouse + vars: + - ears: round + - nose: black