From efde61e53729964f3e740dcbb9c52f889186719d Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 8 May 2012 16:03:51 -0700 Subject: [PATCH 1/2] allow unicode (utf8) characters in jinja templates --- lib/ansible/runner.py | 7 +++++-- lib/ansible/utils.py | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index 135130c5750..31f835585dd 100644 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -29,6 +29,7 @@ import tempfile import time import base64 import getpass +import codecs import ansible.constants as C import ansible.connection @@ -202,7 +203,7 @@ class Runner(object): afd, afile = tempfile.mkstemp() afo = os.fdopen(afd, 'w') - afo.write(data) + afo.write(data.encode("utf8")) afo.flush() afo.close() @@ -512,6 +513,7 @@ class Runner(object): source = utils.template(source, inject, self.setup_cache) + print source (host, ok, data, err) = (None, None, None, None) if not self.is_playbook: @@ -542,7 +544,8 @@ class Runner(object): copy_module = self._transfer_module(conn, tmp, 'copy') # template the source data locally - source_data = file(utils.path_dwim(self.basedir, source)).read() + source_data = codecs.open(utils.path_dwim(self.basedir, source), encoding="utf8").read() + print source_data resultant = '' try: resultant = utils.template(source_data, inject, self.setup_cache) diff --git a/lib/ansible/utils.py b/lib/ansible/utils.py index 1b3b355c43c..a66640ab4f0 100644 --- a/lib/ansible/utils.py +++ b/lib/ansible/utils.py @@ -21,6 +21,7 @@ import sys import os import shlex import re +import codecs import jinja2 import yaml import optparse @@ -233,7 +234,7 @@ def varReplace(raw, vars): def template(text, vars, setup_cache, no_engine=False): ''' run a text buffer through the templating engine ''' vars = vars.copy() - text = varReplace(str(text), vars) + text = varReplace(unicode(text), vars) vars['hostvars'] = setup_cache if no_engine: # used when processing include: directives so that Jinja is evaluated @@ -248,7 +249,7 @@ def double_template(text, vars, setup_cache): def template_from_file(path, vars, setup_cache, no_engine=False): ''' run a file through the templating engine ''' - data = file(path).read() + data = codecs.open(path, encoding="utf8").read() return template(data, vars, setup_cache, no_engine=no_engine) def parse_yaml(data): From b89d8db7ce19821af66cf0ecb36a5363275a6217 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Tue, 8 May 2012 16:05:43 -0700 Subject: [PATCH 2/2] removed debug code --- lib/ansible/runner.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/ansible/runner.py b/lib/ansible/runner.py index 31f835585dd..b66535933f9 100644 --- a/lib/ansible/runner.py +++ b/lib/ansible/runner.py @@ -433,7 +433,6 @@ class Runner(object): # apply templating to source argument inject = self.setup_cache.get(conn.host,{}) - print source source = utils.template(source, inject, self.setup_cache) # files are saved in dest dir, with a subdir for each host, then the filename @@ -513,7 +512,6 @@ class Runner(object): source = utils.template(source, inject, self.setup_cache) - print source (host, ok, data, err) = (None, None, None, None) if not self.is_playbook: @@ -545,7 +543,6 @@ class Runner(object): # template the source data locally source_data = codecs.open(utils.path_dwim(self.basedir, source), encoding="utf8").read() - print source_data resultant = '' try: resultant = utils.template(source_data, inject, self.setup_cache)