From dff33071d02278a95275bd639a4fe8e42797611f Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Wed, 4 Oct 2017 09:26:45 -0400 Subject: [PATCH] check type to avoid typeerror tb fixes #31290 by giving more meaningful message (cherry picked from commit 8aa33419c99632b852a26698c0749ca6714c5296) --- CHANGELOG.md | 1 + lib/ansible/plugins/inventory/yaml.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5c9f82e58..ecfb8a3f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,7 @@ Ansible Changes By Release * inventory_file variable again returns full path, not just basename * added info about cwd group/host vars to porting guide * Fix name parsing out of envra in the yum module +* give user friendly error on badly formatted yaml inventory source diff --git a/lib/ansible/plugins/inventory/yaml.py b/lib/ansible/plugins/inventory/yaml.py index 062f7a235e..3b7777a5b1 100644 --- a/lib/ansible/plugins/inventory/yaml.py +++ b/lib/ansible/plugins/inventory/yaml.py @@ -49,7 +49,6 @@ all: # keys must be unique, i.e. only one 'hosts' per group last_var: MYVALUE ''' -import re import os from collections import MutableMapping @@ -106,8 +105,14 @@ class InventoryModule(BaseFileInventoryPlugin): if isinstance(group_data, MutableMapping): # make sure they are dicts for section in ['vars', 'children', 'hosts']: - if section in group_data and isinstance(group_data[section], string_types): - group_data[section] = {group_data[section]: None} + 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} + + 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): for var in group_data['vars']: