Fix inventory import to look for group and host vars files with .yml, .yaml and .json extensions in addition to no extension.

This commit is contained in:
Chris Church
2014-08-12 22:57:59 -04:00
parent 88c1b05f50
commit 578f4b9c3b
2 changed files with 55 additions and 31 deletions

View File

@@ -53,13 +53,20 @@ class MemObject(object):
self.name = name
self.source_dir = source_dir
def load_vars(self, path):
if os.path.exists(path) and os.path.isfile(path):
def load_vars(self, base_path):
all_vars = {}
for suffix in ('', '.yml', '.yaml', '.json'):
path = ''.join([base_path, suffix])
if not os.path.exists(path):
continue
if not os.path.isfile(path):
continue
vars_name = os.path.basename(os.path.dirname(path))
logger.debug('Loading %s from %s', vars_name, path)
try:
v = yaml.safe_load(file(path, 'r').read())
return v if hasattr(v, 'items') else {}
if hasattr(v, 'items'): # is a dict
all_vars.update(v)
except yaml.YAMLError, e:
if hasattr(e, 'problem_mark'):
logger.error('Invalid YAML in %s:%s col %s', path,
@@ -68,7 +75,7 @@ class MemObject(object):
else:
logger.error('Error loading YAML from %s', path)
raise
return {}
return all_vars
class MemGroup(MemObject):