From 4ee4ddcd7c2f1425b46c824ad5f8f453a31f1966 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 5 Mar 2012 22:00:22 -0500 Subject: [PATCH] Parameterized include statements can see top level variables and also be passed specific variables! Code needs cleanup, but works --- examples/base.yml | 2 ++ examples/playbook.yml | 2 +- lib/ansible/playbook.py | 17 +++++++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/base.yml b/examples/base.yml index 8841afb1d5..888613b2f2 100644 --- a/examples/base.yml +++ b/examples/base.yml @@ -3,3 +3,5 @@ action: command /usr/sbin/setenforce 0 - name: no iptables action: service name=iptables state=stopped +- name: this is just to show variables work here, favcolor={{ favcolor }} + action: command /bin/true diff --git a/examples/playbook.yml b/examples/playbook.yml index 7890c35b06..9fe7cefdd9 100644 --- a/examples/playbook.yml +++ b/examples/playbook.yml @@ -5,7 +5,7 @@ http_port: 80 max_clients: 200 tasks: - - include: base.yml + - include: base.yml favcolor=blue - name: write the apache config file using vars set above action: template src=/srv/httpd.j2 dest=/etc/httpd.conf notify: diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py index 522078abee..b3a99fd892 100755 --- a/lib/ansible/playbook.py +++ b/lib/ansible/playbook.py @@ -23,6 +23,7 @@ from ansible.utils import * import yaml import shlex import os +import jinja2 SETUP_CACHE={ 'foo' : {} } @@ -92,8 +93,20 @@ class PlayBook(object): new_tasks = [] for task in tasks: if 'include' in task: - path = path_dwim(dirname, task['include']) - included = yaml.load(file(path).read()) + # FIXME: refactor + # an include line looks like: + # include: some.yml a=2 b=3 c=4 + include_tokens = task['include'].split() + path = path_dwim(dirname, include_tokens[0]) + inject_vars = play.get('vars', {}) + for i,x in enumerate(include_tokens): + if x.find("=") != -1: + (k,v) = x.split("=") + inject_vars[k] = v + included = file(path).read() + template = jinja2.Template(included) + included = template.render(inject_vars) + included = yaml.load(included) for x in included: new_tasks.append(x) else: