handle groups correctly when they are 'null' (#41698)

* handle groups correctly when they are 'null'

 - even if defined as mapping but having no keys, objects shoudl still be processed correctly
 - also add ansilbe_verbosity to vars not to display in ansible-inventory

fixes #41692
This commit is contained in:
Brian Coca 2018-06-20 11:26:46 -04:00 committed by GitHub
parent e91cee1a31
commit 05a49d6eb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 24 deletions

View file

@ -42,6 +42,7 @@ INTERNAL_VARS = frozenset(['ansible_diff_mode',
'ansible_playbook_python', 'ansible_playbook_python',
'ansible_run_tags', 'ansible_run_tags',
'ansible_skip_tags', 'ansible_skip_tags',
'ansible_verbosity',
'ansible_version', 'ansible_version',
'inventory_dir', 'inventory_dir',
'inventory_file', 'inventory_file',

View file

@ -59,6 +59,7 @@ all: # keys must be unique, i.e. only one 'hosts' per group
''' '''
import os import os
from collections import MutableMapping from collections import MutableMapping
from ansible.errors import AnsibleParserError from ansible.errors import AnsibleParserError
@ -112,37 +113,38 @@ class InventoryModule(BaseFileInventoryPlugin):
def _parse_group(self, group, group_data): def _parse_group(self, group, group_data):
if isinstance(group_data, MutableMapping): if isinstance(group_data, (MutableMapping, type(None))):
self.inventory.add_group(group) self.inventory.add_group(group)
# make sure they are dicts if group_data is not None:
for section in ['vars', 'children', 'hosts']: # make sure they are dicts
if section in group_data: for section in ['vars', 'children', 'hosts']:
# convert strings to dicts as these are allowed if section in group_data:
if isinstance(group_data[section], string_types): # convert strings to dicts as these are allowed
group_data[section] = {group_data[section]: None} if isinstance(group_data[section], string_types):
group_data[section] = {group_data[section]: None}
if not isinstance(group_data[section], MutableMapping): if not isinstance(group_data[section], (MutableMapping, type(None))):
raise AnsibleParserError('Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.' % raise AnsibleParserError('Invalid "%s" entry for "%s" group, requires a dictionary, found "%s" instead.' %
(section, group, type(group_data[section]))) (section, group, type(group_data[section])))
for key in group_data: for key in group_data:
if key == 'vars': if key == 'vars':
for var in group_data['vars']: for var in group_data['vars']:
self.inventory.set_variable(group, var, group_data['vars'][var]) self.inventory.set_variable(group, var, group_data['vars'][var])
elif key == 'children': elif key == 'children':
for subgroup in group_data['children']: for subgroup in group_data['children']:
self._parse_group(subgroup, group_data['children'][subgroup]) self._parse_group(subgroup, group_data['children'][subgroup])
self.inventory.add_child(group, subgroup) self.inventory.add_child(group, subgroup)
elif key == 'hosts': elif key == 'hosts':
for host_pattern in group_data['hosts']: for host_pattern in group_data['hosts']:
hosts, port = self._parse_host(host_pattern) hosts, port = self._parse_host(host_pattern)
self._populate_host_vars(hosts, group_data['hosts'][host_pattern] or {}, group, port) self._populate_host_vars(hosts, group_data['hosts'][host_pattern] or {}, group, port)
else: else:
self.display.warning('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group)) self.display.warning('Skipping unexpected key (%s) in group (%s), only "vars", "children" and "hosts" are valid' % (key, group))
else: else:
self.display.warning("Skipping '%s' as this is not a valid group definition" % group) self.display.warning("Skipping '%s' as this is not a valid group definition" % group)