Add support for role defaults. These are variables on a per role basis with lowest precedence.
This commit is contained in:
parent
49b0ff18a7
commit
494043947d
1 changed files with 25 additions and 0 deletions
|
@ -183,6 +183,20 @@ class Play(object):
|
||||||
dep_stack.append([role,role_path,role_vars])
|
dep_stack.append([role,role_path,role_vars])
|
||||||
return dep_stack
|
return dep_stack
|
||||||
|
|
||||||
|
def _load_role_defaults(self, defaults_files):
|
||||||
|
# process default variables
|
||||||
|
default_vars = {}
|
||||||
|
for filename in defaults_files:
|
||||||
|
if os.path.exists(filename):
|
||||||
|
new_default_vars = utils.parse_yaml_from_file(filename)
|
||||||
|
if new_default_vars:
|
||||||
|
if type(new_default_vars) != dict:
|
||||||
|
raise errors.AnsibleError("%s must be stored as dictonary/hash: %s" % (filename, type(new_default_vars)))
|
||||||
|
|
||||||
|
default_vars = utils.combine_vars(default_vars, new_default_vars)
|
||||||
|
|
||||||
|
return default_vars
|
||||||
|
|
||||||
def _load_roles(self, roles, ds):
|
def _load_roles(self, roles, ds):
|
||||||
# a role is a name that auto-includes the following if they exist
|
# a role is a name that auto-includes the following if they exist
|
||||||
# <rolename>/tasks/main.yml
|
# <rolename>/tasks/main.yml
|
||||||
|
@ -199,6 +213,7 @@ class Play(object):
|
||||||
new_tasks = []
|
new_tasks = []
|
||||||
new_handlers = []
|
new_handlers = []
|
||||||
new_vars_files = []
|
new_vars_files = []
|
||||||
|
defaults_files = []
|
||||||
|
|
||||||
pre_tasks = ds.get('pre_tasks', None)
|
pre_tasks = ds.get('pre_tasks', None)
|
||||||
if type(pre_tasks) != list:
|
if type(pre_tasks) != list:
|
||||||
|
@ -222,10 +237,13 @@ class Play(object):
|
||||||
task_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'tasks'))
|
task_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'tasks'))
|
||||||
handler_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'handlers'))
|
handler_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'handlers'))
|
||||||
vars_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'vars'))
|
vars_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'vars'))
|
||||||
|
defaults_basepath = utils.path_dwim(self.basedir, os.path.join(role_path, 'defaults'))
|
||||||
|
|
||||||
task = self._resolve_main(task_basepath)
|
task = self._resolve_main(task_basepath)
|
||||||
handler = self._resolve_main(handler_basepath)
|
handler = self._resolve_main(handler_basepath)
|
||||||
vars_file = self._resolve_main(vars_basepath)
|
vars_file = self._resolve_main(vars_basepath)
|
||||||
|
defaults_file = self._resolve_main(defaults_basepath)
|
||||||
|
|
||||||
library = utils.path_dwim(self.basedir, os.path.join(role_path, 'library'))
|
library = utils.path_dwim(self.basedir, os.path.join(role_path, 'library'))
|
||||||
|
|
||||||
if not os.path.isfile(task) and not os.path.isfile(handler) and not os.path.isfile(vars_file) and not os.path.isdir(library):
|
if not os.path.isfile(task) and not os.path.isfile(handler) and not os.path.isfile(vars_file) and not os.path.isdir(library):
|
||||||
|
@ -244,6 +262,8 @@ class Play(object):
|
||||||
new_handlers.append(nt)
|
new_handlers.append(nt)
|
||||||
if os.path.isfile(vars_file):
|
if os.path.isfile(vars_file):
|
||||||
new_vars_files.append(vars_file)
|
new_vars_files.append(vars_file)
|
||||||
|
if os.path.isfile(defaults_file):
|
||||||
|
defaults_files.append(defaults_file)
|
||||||
if os.path.isdir(library):
|
if os.path.isdir(library):
|
||||||
utils.plugins.module_finder.add_directory(library)
|
utils.plugins.module_finder.add_directory(library)
|
||||||
|
|
||||||
|
@ -275,6 +295,11 @@ class Play(object):
|
||||||
ds['handlers'] = new_handlers
|
ds['handlers'] = new_handlers
|
||||||
ds['vars_files'] = new_vars_files
|
ds['vars_files'] = new_vars_files
|
||||||
|
|
||||||
|
defaults = self._load_role_defaults(defaults_files)
|
||||||
|
# merge default vars with self.vars, with vars taking precedence.
|
||||||
|
if defaults:
|
||||||
|
self.vars = utils.combine_vars(defaults, self.vars)
|
||||||
|
|
||||||
return ds
|
return ds
|
||||||
|
|
||||||
# *************************************************
|
# *************************************************
|
||||||
|
|
Loading…
Reference in a new issue