From 3ab0ed5fd4b2f9f1cfb47a72c90ed7a4b93e1983 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 12 Apr 2013 19:02:56 -0400 Subject: [PATCH] Allow 'when' and 'with_items' to be applied to roles. --- examples/playbooks/roles/foo/handlers/main.yml | 2 +- lib/ansible/playbook/play.py | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/examples/playbooks/roles/foo/handlers/main.yml b/examples/playbooks/roles/foo/handlers/main.yml index e8a60de47db..030b30f5e3a 100644 --- a/examples/playbooks/roles/foo/handlers/main.yml +++ b/examples/playbooks/roles/foo/handlers/main.yml @@ -6,5 +6,5 @@ # within a role, it's possible to include other task files as well. By default, we # can reference files in the same directory without doing anything special: -- include: other.yml +# - include: other.yml diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index f815a016ee4..3a65aae52b9 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -140,6 +140,9 @@ class Play(object): has_dict = orig_path orig_path = role_name + with_items = has_dict.get('with_items', None) + when = has_dict.get('when', None) + 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)) @@ -152,9 +155,19 @@ 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, vars=has_dict)) + nt = dict(include=task, vars=has_dict) + if when: + nt['when'] = when + if with_items: + nt['with_items'] = with_items + new_tasks.append(nt) if os.path.isfile(handler): - new_handlers.append(dict(include=handler, vars=has_dict)) + nt = dict(include=handler, vars=has_dict) + if when: + nt['when'] = when + if with_items: + nt['with_items'] = with_items + new_handlers.append(nt) if os.path.isfile(vars_file): new_vars_files.append(vars_file)