From addedb12cf6e95483b8cc92190dce369da47ecae Mon Sep 17 00:00:00 2001 From: Pilou Date: Sun, 5 Mar 2017 10:24:41 +0100 Subject: [PATCH] Inventory default groups 'all' and 'ungrouped': add tests and documentation (#21728) * inventory: test 'all' & 'ungrouped' groups created by default * Mention default groups 'all' & 'ungrouped' * Update intro_inventory.rst Minor grammatical edit. --- docs/docsite/rst/intro_inventory.rst | 9 ++++ test/units/inventory/test_inventory.py | 60 +++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/docs/docsite/rst/intro_inventory.rst b/docs/docsite/rst/intro_inventory.rst index 88ed1e4b3a..cf3b103f91 100644 --- a/docs/docsite/rst/intro_inventory.rst +++ b/docs/docsite/rst/intro_inventory.rst @@ -141,6 +141,15 @@ It is also possible to make groups of groups using the ``:children`` suffix. Jus If you need to store lists or hash data, or prefer to keep host and group specific variables separate from the inventory file, see the next section. +.. _default_groups: + +Default groups +++++++++++++++ + +There are two default groups: ``all`` and ``ungrouped``. ``all`` contains every host. +``ungrouped`` contains all hosts declared without an explicit section, even if they belong to +another group. + .. _splitting_out_vars: Splitting Out Host and Group Specific Data diff --git a/test/units/inventory/test_inventory.py b/test/units/inventory/test_inventory.py index 42f488aa37..ff76394650 100644 --- a/test/units/inventory/test_inventory.py +++ b/test/units/inventory/test_inventory.py @@ -22,9 +22,8 @@ __metaclass__ = type import string from ansible.compat.tests import unittest -from ansible.compat.tests.mock import patch, MagicMock +from ansible.compat.tests.mock import patch -from ansible.errors import AnsibleError, AnsibleParserError from ansible.inventory import Inventory from ansible.inventory.expand_hosts import expand_hostname_range from ansible.vars import VariableManager @@ -115,3 +114,60 @@ class TestInventory(unittest.TestCase): for e in self.ranges_to_expand: r = self.ranges_to_expand[e] self.assertEqual(r, expand_hostname_range(e)) + + +class InventoryDefaultGroup(unittest.TestCase): + + def test_empty_inventory(self): + inventory = self._get_inventory('') + + self.assertIn('all', inventory.groups) + self.assertIn('ungrouped', inventory.groups) + self.assertFalse(inventory.groups['all'].get_hosts()) + self.assertFalse(inventory.groups['ungrouped'].get_hosts()) + + def test_ini(self): + self._test_default_groups(""" + host1 + host2 + host3 + [servers] + host3 + host4 + host5 + """) + + def test_ini_explicit_ungrouped(self): + self._test_default_groups(""" + [ungrouped] + host1 + host2 + host3 + [servers] + host3 + host4 + host5 + """) + + def _get_inventory(self, inventory_content): + v = VariableManager() + + fake_loader = DictDataLoader({ + 'hosts': inventory_content + }) + + with patch.object(Inventory, 'basedir') as mock_basedir: + mock_basedir.return_value = './' + return Inventory(loader=fake_loader, variable_manager=v, host_list='hosts') + + def _test_default_groups(self, inventory_content): + inventory = self._get_inventory(inventory_content) + + self.assertIn('all', inventory.groups) + self.assertIn('ungrouped', inventory.groups) + all_hosts = set(host.name for host in inventory.groups['all'].get_hosts()) + self.assertEqual(set(['host1', 'host2', 'host3', 'host4', 'host5']), all_hosts) + ungrouped_hosts = set(host.name for host in inventory.groups['ungrouped'].get_hosts()) + self.assertEqual(set(['host1', 'host2', 'host3']), ungrouped_hosts) + servers_hosts = set(host.name for host in inventory.groups['servers'].get_hosts()) + self.assertEqual(set(['host3', 'host4', 'host5']), servers_hosts)