1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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.
This commit is contained in:
Pilou 2017-03-05 10:24:41 +01:00 committed by scottb
parent 0775d39a87
commit addedb12cf
2 changed files with 67 additions and 2 deletions

View file

@ -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 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. 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_vars:
Splitting Out Host and Group Specific Data Splitting Out Host and Group Specific Data

View file

@ -22,9 +22,8 @@ __metaclass__ = type
import string import string
from ansible.compat.tests import unittest 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 import Inventory
from ansible.inventory.expand_hosts import expand_hostname_range from ansible.inventory.expand_hosts import expand_hostname_range
from ansible.vars import VariableManager from ansible.vars import VariableManager
@ -115,3 +114,60 @@ class TestInventory(unittest.TestCase):
for e in self.ranges_to_expand: for e in self.ranges_to_expand:
r = self.ranges_to_expand[e] r = self.ranges_to_expand[e]
self.assertEqual(r, expand_hostname_range(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)