Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow unicode (utf8) in templates #339

Merged
merged 2 commits into from
May 10, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/ansible/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import time
import base64
import getpass
import codecs

import ansible.constants as C
import ansible.connection
Expand Down Expand Up @@ -202,7 +203,7 @@ def _transfer_str(self, conn, tmp, name, data):

afd, afile = tempfile.mkstemp()
afo = os.fdopen(afd, 'w')
afo.write(data)
afo.write(data.encode("utf8"))
afo.flush()
afo.close()

Expand Down Expand Up @@ -432,7 +433,6 @@ def _execute_fetch(self, conn, host, tmp):

# 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
Expand Down Expand Up @@ -542,7 +542,7 @@ def _execute_template(self, conn, host, tmp):
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()
resultant = ''
try:
resultant = utils.template(source_data, inject, self.setup_cache)
Expand Down
5 changes: 3 additions & 2 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os
import shlex
import re
import codecs
import jinja2
import yaml
import optparse
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down