Merge pull request #339 from mgwilliams/feature-unicode

Allow unicode (utf8) in templates
This commit is contained in:
Michael DeHaan 2012-05-09 17:16:59 -07:00
commit 9b587d4458
2 changed files with 6 additions and 5 deletions

View file

@ -29,6 +29,7 @@ import tempfile
import time import time
import base64 import base64
import getpass import getpass
import codecs
import ansible.constants as C import ansible.constants as C
import ansible.connection import ansible.connection
@ -202,7 +203,7 @@ class Runner(object):
afd, afile = tempfile.mkstemp() afd, afile = tempfile.mkstemp()
afo = os.fdopen(afd, 'w') afo = os.fdopen(afd, 'w')
afo.write(data) afo.write(data.encode("utf8"))
afo.flush() afo.flush()
afo.close() afo.close()
@ -432,7 +433,6 @@ class Runner(object):
# apply templating to source argument # apply templating to source argument
inject = self.setup_cache.get(conn.host,{}) inject = self.setup_cache.get(conn.host,{})
print source
source = utils.template(source, inject, self.setup_cache) source = utils.template(source, inject, self.setup_cache)
# files are saved in dest dir, with a subdir for each host, then the filename # files are saved in dest dir, with a subdir for each host, then the filename
@ -542,7 +542,7 @@ class Runner(object):
copy_module = self._transfer_module(conn, tmp, 'copy') copy_module = self._transfer_module(conn, tmp, 'copy')
# template the source data locally # 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()
resultant = '' resultant = ''
try: try:
resultant = utils.template(source_data, inject, self.setup_cache) resultant = utils.template(source_data, inject, self.setup_cache)

View file

@ -21,6 +21,7 @@ import sys
import os import os
import shlex import shlex
import re import re
import codecs
import jinja2 import jinja2
import yaml import yaml
import optparse import optparse
@ -233,7 +234,7 @@ def varReplace(raw, vars):
def template(text, vars, setup_cache, no_engine=False): def template(text, vars, setup_cache, no_engine=False):
''' run a text buffer through the templating engine ''' ''' run a text buffer through the templating engine '''
vars = vars.copy() vars = vars.copy()
text = varReplace(str(text), vars) text = varReplace(unicode(text), vars)
vars['hostvars'] = setup_cache vars['hostvars'] = setup_cache
if no_engine: if no_engine:
# used when processing include: directives so that Jinja is evaluated # used when processing include: directives so that Jinja is evaluated
@ -251,7 +252,7 @@ def double_template(text, vars, setup_cache):
def template_from_file(path, vars, setup_cache, no_engine=False): def template_from_file(path, vars, setup_cache, no_engine=False):
''' run a file through the templating engine ''' ''' 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) return template(data, vars, setup_cache, no_engine=no_engine)
def parse_yaml(data): def parse_yaml(data):