check type to avoid typeerror tb

fixes #31290 by giving more meaningful message

(cherry picked from commit 8aa33419c9)
pull/31343/head
Brian Coca 7 years ago
parent 64b569635d
commit dff33071d0

@ -93,6 +93,7 @@ Ansible Changes By Release
* inventory_file variable again returns full path, not just basename * inventory_file variable again returns full path, not just basename
* added info about cwd group/host vars to porting guide * added info about cwd group/host vars to porting guide
* Fix name parsing out of envra in the yum module * Fix name parsing out of envra in the yum module
* give user friendly error on badly formatted yaml inventory source
<a id="2.4"></a> <a id="2.4"></a>

@ -49,7 +49,6 @@ all: # keys must be unique, i.e. only one 'hosts' per group
last_var: MYVALUE last_var: MYVALUE
''' '''
import re
import os import os
from collections import MutableMapping from collections import MutableMapping
@ -106,9 +105,15 @@ class InventoryModule(BaseFileInventoryPlugin):
if isinstance(group_data, MutableMapping): if isinstance(group_data, MutableMapping):
# make sure they are dicts # make sure they are dicts
for section in ['vars', 'children', 'hosts']: for section in ['vars', 'children', 'hosts']:
if section in group_data and isinstance(group_data[section], string_types): if section in group_data:
# convert strings to dicts as these are allowed
if isinstance(group_data[section], string_types):
group_data[section] = {group_data[section]: None} group_data[section] = {group_data[section]: None}
if not isinstance(group_data[section], MutableMapping):
raise AnsibleParserError('Invalid %s entry for %s group, requires a dictionary, found %s instead.' %
(section, group, type(group_data[section])))
if group_data.get('vars', False): if group_data.get('vars', False):
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])

Loading…
Cancel
Save