Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit addedb1

Browse files
pilou-dharmabumstead
authored andcommitted
Inventory default groups 'all' and 'ungrouped': add tests and documentation (ansible#21728)
* inventory: test 'all' & 'ungrouped' groups created by default * Mention default groups 'all' & 'ungrouped' * Update intro_inventory.rst Minor grammatical edit.
1 parent 0775d39 commit addedb1

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

docs/docsite/rst/intro_inventory.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ It is also possible to make groups of groups using the ``:children`` suffix. Jus
141141
If you need to store lists or hash data, or prefer to keep host and group specific variables
142142
separate from the inventory file, see the next section.
143143

144+
.. _default_groups:
145+
146+
Default groups
147+
++++++++++++++
148+
149+
There are two default groups: ``all`` and ``ungrouped``. ``all`` contains every host.
150+
``ungrouped`` contains all hosts declared without an explicit section, even if they belong to
151+
another group.
152+
144153
.. _splitting_out_vars:
145154

146155
Splitting Out Host and Group Specific Data

test/units/inventory/test_inventory.py

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import string
2323

2424
from ansible.compat.tests import unittest
25-
from ansible.compat.tests.mock import patch, MagicMock
25+
from ansible.compat.tests.mock import patch
2626

27-
from ansible.errors import AnsibleError, AnsibleParserError
2827
from ansible.inventory import Inventory
2928
from ansible.inventory.expand_hosts import expand_hostname_range
3029
from ansible.vars import VariableManager
@@ -115,3 +114,60 @@ def test_expand_hostname_range(self):
115114
for e in self.ranges_to_expand:
116115
r = self.ranges_to_expand[e]
117116
self.assertEqual(r, expand_hostname_range(e))
117+
118+
119+
class InventoryDefaultGroup(unittest.TestCase):
120+
121+
def test_empty_inventory(self):
122+
inventory = self._get_inventory('')
123+
124+
self.assertIn('all', inventory.groups)
125+
self.assertIn('ungrouped', inventory.groups)
126+
self.assertFalse(inventory.groups['all'].get_hosts())
127+
self.assertFalse(inventory.groups['ungrouped'].get_hosts())
128+
129+
def test_ini(self):
130+
self._test_default_groups("""
131+
host1
132+
host2
133+
host3
134+
[servers]
135+
host3
136+
host4
137+
host5
138+
""")
139+
140+
def test_ini_explicit_ungrouped(self):
141+
self._test_default_groups("""
142+
[ungrouped]
143+
host1
144+
host2
145+
host3
146+
[servers]
147+
host3
148+
host4
149+
host5
150+
""")
151+
152+
def _get_inventory(self, inventory_content):
153+
v = VariableManager()
154+
155+
fake_loader = DictDataLoader({
156+
'hosts': inventory_content
157+
})
158+
159+
with patch.object(Inventory, 'basedir') as mock_basedir:
160+
mock_basedir.return_value = './'
161+
return Inventory(loader=fake_loader, variable_manager=v, host_list='hosts')
162+
163+
def _test_default_groups(self, inventory_content):
164+
inventory = self._get_inventory(inventory_content)
165+
166+
self.assertIn('all', inventory.groups)
167+
self.assertIn('ungrouped', inventory.groups)
168+
all_hosts = set(host.name for host in inventory.groups['all'].get_hosts())
169+
self.assertEqual(set(['host1', 'host2', 'host3', 'host4', 'host5']), all_hosts)
170+
ungrouped_hosts = set(host.name for host in inventory.groups['ungrouped'].get_hosts())
171+
self.assertEqual(set(['host1', 'host2', 'host3']), ungrouped_hosts)
172+
servers_hosts = set(host.name for host in inventory.groups['servers'].get_hosts())
173+
self.assertEqual(set(['host3', 'host4', 'host5']), servers_hosts)

0 commit comments

Comments
 (0)