allow include_role to specify vars/defaults files

This commit is contained in:
Brian Coca 2016-08-26 21:54:06 -04:00
parent 4b679ffd84
commit 4e6a7a9e01
2 changed files with 8 additions and 6 deletions

View file

@ -85,7 +85,7 @@ class Role(Base, Become, Conditional, Taggable):
if from_files is None: if from_files is None:
from_files = {} from_files = {}
self._tasks_from = from_files.get('tasks') self._from_files = from_files
super(Role, self).__init__() super(Role, self).__init__()
@ -174,7 +174,7 @@ class Role(Base, Become, Conditional, Taggable):
else: else:
self._metadata = RoleMetadata() self._metadata = RoleMetadata()
task_data = self._load_role_yaml('tasks', main=self._tasks_from) task_data = self._load_role_yaml('tasks', main=self._from_files.get('tasks'))
if task_data: if task_data:
try: try:
self._task_blocks = load_list_of_blocks(task_data, play=self._play, role=self, loader=self._loader, variable_manager=self._variable_manager) self._task_blocks = load_list_of_blocks(task_data, play=self._play, role=self, loader=self._loader, variable_manager=self._variable_manager)
@ -189,13 +189,13 @@ class Role(Base, Become, Conditional, Taggable):
raise AnsibleParserError("The handlers/main.yml file for role '%s' must contain a list of tasks" % self._role_name , obj=handler_data) raise AnsibleParserError("The handlers/main.yml file for role '%s' must contain a list of tasks" % self._role_name , obj=handler_data)
# vars and default vars are regular dictionaries # vars and default vars are regular dictionaries
self._role_vars = self._load_role_yaml('vars') self._role_vars = self._load_role_yaml('vars', main=self._from_files.get('vars'))
if self._role_vars is None: if self._role_vars is None:
self._role_vars = dict() self._role_vars = dict()
elif not isinstance(self._role_vars, dict): elif not isinstance(self._role_vars, dict):
raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name) raise AnsibleParserError("The vars/main.yml file for role '%s' must contain a dictionary of variables" % self._role_name)
self._default_vars = self._load_role_yaml('defaults') self._default_vars = self._load_role_yaml('defaults', main=self._from_files.get('defaults'))
if self._default_vars is None: if self._default_vars is None:
self._default_vars = dict() self._default_vars = dict()
elif not isinstance(self._default_vars, dict): elif not isinstance(self._default_vars, dict):

View file

@ -63,8 +63,10 @@ class IncludeRole(Task):
# build options for roles # build options for roles
from_files = {} from_files = {}
if args.get('tasks_from'): for key in ['tasks', 'vars', 'defaults']:
from_files['tasks'] = basename(args.get('tasks_from')) from_key = key + '_from'
if args.get(from_key):
from_files[key] = basename(args.get(from_key))
#build role #build role
actual_role = Role.load(ri, block._play, parent_role=role, from_files=from_files) actual_role = Role.load(ri, block._play, parent_role=role, from_files=from_files)