Allow role names to be templated

Fixes #10858
This commit is contained in:
James Cammarata 2015-08-03 16:25:08 -04:00
parent ed603f7030
commit 8279557e8f
5 changed files with 22 additions and 9 deletions

View file

@ -98,7 +98,7 @@ def load_list_of_tasks(ds, play, block=None, role=None, task_include=None, use_h
return task_list
def load_list_of_roles(ds, current_role_path=None, variable_manager=None, loader=None):
def load_list_of_roles(ds, play, current_role_path=None, variable_manager=None, loader=None):
'''
Loads and returns a list of RoleInclude objects from the datastructure
list of role definitions
@ -112,7 +112,7 @@ def load_list_of_roles(ds, current_role_path=None, variable_manager=None, loader
roles = []
for role_def in ds:
i = RoleInclude.load(role_def, current_role_path=current_role_path, variable_manager=variable_manager, loader=loader)
i = RoleInclude.load(role_def, play=play, current_role_path=current_role_path, variable_manager=variable_manager, loader=loader)
roles.append(i)
return roles

View file

@ -211,7 +211,7 @@ class Play(Base, Taggable, Become):
if ds is None:
ds = []
role_includes = load_list_of_roles(ds, variable_manager=self._variable_manager, loader=self._loader)
role_includes = load_list_of_roles(ds, play=self, variable_manager=self._variable_manager, loader=self._loader)
roles = []
for ri in role_includes:

View file

@ -31,6 +31,7 @@ from ansible.playbook.base import Base
from ansible.playbook.become import Become
from ansible.playbook.conditional import Conditional
from ansible.playbook.taggable import Taggable
from ansible.template import Templar
from ansible.utils.path import unfrackpath
@ -41,7 +42,11 @@ class RoleDefinition(Base, Become, Conditional, Taggable):
_role = FieldAttribute(isa='string')
def __init__(self, role_basedir=None):
def __init__(self, play=None, role_basedir=None, variable_manager=None, loader=None):
self._play = play
self._variable_manager = variable_manager
self._loader = loader
self._role_path = None
self._role_basedir = role_basedir
self._role_params = dict()
@ -112,6 +117,14 @@ class RoleDefinition(Base, Become, Conditional, Taggable):
if not role_name or not isinstance(role_name, string_types):
raise AnsibleError('role definitions must contain a role name', obj=ds)
# if we have the required datastructures, and if the role_name
# contains a variable, try and template it now
if self._play and self._variable_manager:
all_vars = self._variable_manager.get_vars(loader=self._loader, play=self._play)
templar = Templar(loader=self._loader, variables=all_vars)
if templar._contains_vars(role_name):
role_name = templar.template(role_name)
return role_name
def _load_role_path(self, role_name):

View file

@ -38,14 +38,14 @@ class RoleInclude(RoleDefinition):
FIXME: docstring
"""
def __init__(self, role_basedir=None):
super(RoleInclude, self).__init__(role_basedir=role_basedir)
def __init__(self, play=None, role_basedir=None, variable_manager=None, loader=None):
super(RoleInclude, self).__init__(play=play, role_basedir=role_basedir, variable_manager=variable_manager, loader=loader)
@staticmethod
def load(data, current_role_path=None, parent_role=None, variable_manager=None, loader=None):
def load(data, play, current_role_path=None, parent_role=None, variable_manager=None, loader=None):
assert isinstance(data, string_types) or isinstance(data, dict) or isinstance(data, AnsibleBaseYAMLObject)
ri = RoleInclude(role_basedir=current_role_path)
ri = RoleInclude(play=play, role_basedir=current_role_path, variable_manager=variable_manager, loader=loader)
return ri.load_data(data, variable_manager=variable_manager, loader=loader)

View file

@ -72,7 +72,7 @@ class RoleMetadata(Base):
if self._owner:
current_role_path = os.path.dirname(self._owner._role_path)
return load_list_of_roles(ds, current_role_path=current_role_path, variable_manager=self._variable_manager, loader=self._loader)
return load_list_of_roles(ds, play=self._owner._play, current_role_path=current_role_path, variable_manager=self._variable_manager, loader=self._loader)
def _load_galaxy_info(self, attr, ds):
'''