diff --git a/examples/playbooks/roles/foo/tasks/main.yml b/examples/playbooks/roles/foo/tasks/main.yml index 13a143dc9f..1642311f51 100644 --- a/examples/playbooks/roles/foo/tasks/main.yml +++ b/examples/playbooks/roles/foo/tasks/main.yml @@ -7,3 +7,8 @@ template: src=foo.j2 dest=/tmp/roles_test2.txt notify: - blippy + +- name: demo that parameterized roles work + shell: echo just FYI, param1={{ param1 }}, param2 ={{ param2 }} + + diff --git a/examples/playbooks/roletest.yml b/examples/playbooks/roletest.yml index 8aa934588f..ec9c9886c3 100644 --- a/examples/playbooks/roletest.yml +++ b/examples/playbooks/roletest.yml @@ -26,13 +26,21 @@ - hosts: all roles: - - foo + + # a role can be listed flat like this: + # + # - common + # - webservers + + # but you can also pass variables to them, so they can be parameterized. You can call + # a role more than once with different parameters too. It might look like this: + + - { role: foo, param1: 1000, param2: 2000 } + - { role: foo, param1: 8000, param2: 9000 } # add as many roles as you like, roles takes a list of roles names # these paths can be qualified, but if bare, it will look from them in # roles/$rolename relative to the playbook - # - # - bar # explicit tasks and handlers can be used, but are not required. # they will run after the roles if present. diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 7c5a746255..74ef90e2e4 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -125,7 +125,20 @@ class Play(object): new_tasks = [] new_handlers = [] new_vars_files = [] + + # variables if the role was parameterized (i.e. given as a hash) + has_dict = {} + for orig_path in roles: + + if type(orig_path) == dict: + # what, not a path? + role_name = orig_path.get('role', None) + if role_name is None: + raise errors.AnsibleError("expected a role name in dictionary: %s" % orig_path) + has_dict = orig_path + orig_path = role_name + path = utils.path_dwim(self.basedir, orig_path) if not os.path.isdir(path) and not orig_path.startswith(".") and not orig_path.startswith("/"): path2 = utils.path_dwim(self.basedir, os.path.join('roles', orig_path)) @@ -138,9 +151,9 @@ class Play(object): handler = utils.path_dwim(self.basedir, os.path.join(path, 'handlers', 'main.yml')) vars_file = utils.path_dwim(self.basedir, os.path.join(path, 'vars', 'main.yml')) if os.path.isfile(task): - new_tasks.append(dict(include=task)) + new_tasks.append(dict(include=task, vars=has_dict)) if os.path.isfile(handler): - new_handlers.append(dict(include=handler)) + new_handlers.append(dict(include=handler, vars=has_dict)) if os.path.isfile(vars_file): new_vars_files.append(vars_file) @@ -155,6 +168,7 @@ class Play(object): vars_files = [] tasks.extend(new_tasks) handlers.extend(new_handlers) + vars_files.extend(new_vars_files) ds['tasks'] = tasks ds['handlers'] = handlers