From af9596307d1dcea7c8c4c6657456137347662db9 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 19 Mar 2012 19:23:14 -0400 Subject: [PATCH] Move templating into a utils function. Reuse is our friend. --- lib/ansible/playbook.py | 9 ++------- lib/ansible/runner.py | 4 +--- lib/ansible/utils.py | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py index 282f2828efd..333e2278a91 100755 --- a/lib/ansible/playbook.py +++ b/lib/ansible/playbook.py @@ -24,7 +24,6 @@ from ansible import errors import yaml import shlex import os -import jinja2 import time # used to transfer variables to Runner @@ -111,19 +110,15 @@ class PlayBook(object): 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 = utils.template_from_file(path, inject_vars) included = yaml.load(included) for x in included: new_tasks.append(x) def _include_handlers(self, play, handler, dirname, new_handlers): path = utils.path_dwim(dirname, handler['include']) - included = file(path).read() inject_vars = self._get_vars(play, dirname) - template = jinja2.Template(included) - included = template.render(inject_vars) + included = utils.template_from_file(path, inject_vars) included = yaml.load(included) for x in included: new_handlers.append(x) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index f423ac26806..6698aa569fa 100755 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -24,7 +24,6 @@ import signal import os import Queue import random -import jinja2 import traceback import tempfile import subprocess @@ -326,8 +325,7 @@ class Runner(object): else: args = "%s metadata=~/.ansible/setup" % args - template = jinja2.Template(args) - args = template.render(inject_vars) + args = utils.template(args, inject_vars) argsfile = self._transfer_argsfile(conn, tmp, args) if async_jid is None: diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 993762e6e75..396f63298d0 100755 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -20,13 +20,15 @@ import sys import os import shlex -from ansible import errors +import jinja2 try: import json except ImportError: import simplejson as json +from ansible import errors + ############################################################### # UTILITY FUNCTIONS FOR COMMAND LINE TOOLS ############################################################### @@ -222,4 +224,15 @@ def parse_json(data): return { "failed" : True, "parsed" : False, "msg" : data } return results +def template(text, vars): + ''' run a text buffer through the templating engine ''' + template = jinja2.Template(text) + return template.render(vars) + +def template_from_file(path, vars): + ''' run a file through the templating engine ''' + data = file(path).read() + return template(data, vars) + +