do not error in case of empty list constructed group (#53748)

This commit is contained in:
Alan Rominger 2019-03-14 12:27:56 -04:00 committed by Brian Coca
parent 14d670573a
commit 8c2037aaa9
2 changed files with 23 additions and 7 deletions

View file

@ -429,7 +429,9 @@ class Constructable(object):
self.inventory.add_child(parent_name, result_gname) self.inventory.add_child(parent_name, result_gname)
else: else:
if strict: # exclude case of empty list and dictionary, because these are valid constructions
raise AnsibleParserError("No key or key resulted empty, invalid entry") # simply no groups need to be constructed, but are still falsy
if strict and key not in ([], {}):
raise AnsibleParserError("No key or key resulted empty for %s in host %s, invalid entry" % (keyed.get('key'), host))
else: else:
raise AnsibleParserError("Invalid keyed group entry, it must be a dictionary: %s " % keyed) raise AnsibleParserError("Invalid keyed group entry, it must be a dictionary: %s " % keyed)

View file

@ -24,7 +24,7 @@ from ansible.inventory.data import InventoryData
from ansible.template import Templar from ansible.template import Templar
@pytest.fixture(scope="module") @pytest.fixture()
def inventory_module(): def inventory_module():
r = InventoryModule() r = InventoryModule()
r.inventory = InventoryData() r.inventory = InventoryData()
@ -60,13 +60,11 @@ def test_keyed_group_separator(inventory_module):
{ {
'prefix': 'farmer', 'prefix': 'farmer',
'separator': '_old_', 'separator': '_old_',
'key': 'farmer', 'key': 'farmer'
'unsafe': True
}, },
{ {
'separator': 'mmmmmmmmmm', 'separator': 'mmmmmmmmmm',
'key': 'barn', 'key': 'barn'
'unsafe': True
} }
] ]
inventory_module._add_host_to_keyed_groups( inventory_module._add_host_to_keyed_groups(
@ -78,6 +76,22 @@ def test_keyed_group_separator(inventory_module):
assert group.hosts == [host] assert group.hosts == [host]
def test_keyed_group_empty_construction(inventory_module):
inventory_module.inventory.add_host('farm')
inventory_module.inventory.set_variable('farm', 'barn', {})
host = inventory_module.inventory.get_host('farm')
keyed_groups = [
{
'separator': 'mmmmmmmmmm',
'key': 'barn'
}
]
inventory_module._add_host_to_keyed_groups(
keyed_groups, host.vars, host.name, strict=True
)
assert host.groups == []
def test_keyed_parent_groups(inventory_module): def test_keyed_parent_groups(inventory_module):
inventory_module.inventory.add_host('web1') inventory_module.inventory.add_host('web1')
inventory_module.inventory.add_host('web2') inventory_module.inventory.add_host('web2')