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

Create a Jinja2 environment allowing includes #488

Merged
merged 2 commits into from
Jun 19, 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
3 changes: 1 addition & 2 deletions lib/ansible/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,7 @@ def _execute_template(self, conn, tmp):

# template the source data locally
try:
resultant = utils.template_from_file(utils.path_dwim(self.basedir, source),
inject, self.setup_cache, no_engine=False)
resultant = utils.template_from_file(self.basedir, source, inject, self.setup_cache)
except Exception, e:
result = dict(failed=True, msg=str(e))
return ReturnData(host=conn.host, comm_ok=False, result=result)
Expand Down
30 changes: 14 additions & 16 deletions lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,35 +265,33 @@ def varReplace(raw, vars):

return ''.join(done)

def _template(text, vars, setup_cache=None, no_engine=True):
def _template(text, vars, setup_cache=None):
''' run a text buffer through the templating engine '''
vars = vars.copy()
vars['hostvars'] = setup_cache
text = varReplace(unicode(text), vars)
if no_engine:
# used when processing include: directives so that Jinja is evaluated
# in a later context when more variables are available
return text
else:
template = jinja2.Template(text)
res = template.render(vars)
if text.endswith('\n') and not res.endswith('\n'):
res = res + '\n'
return res
return text

def template(text, vars, setup_cache=None, no_engine=True):
def template(text, vars, setup_cache=None):
''' run a text buffer through the templating engine
until it no longer changes '''
prev_text = ''
while prev_text != text:
prev_text = text
text = _template(text, vars, setup_cache, no_engine)
text = _template(text, vars, setup_cache)
return text

def template_from_file(path, vars, setup_cache, no_engine=True):
def template_from_file(basedir, path, vars, setup_cache):
''' run a file through the templating engine '''
data = codecs.open(path, encoding="utf8").read()
return template(data, vars, setup_cache, no_engine=no_engine)
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(basedir), trim_blocks=False)
data = codecs.open(path_dwim(basedir, path), encoding="utf8").read()
template = environment.from_string(data)
vars = vars.copy()
vars['hostvars'] = setup_cache
res = template.render(vars)
if data.endswith('\n') and not res.endswith('\n'):
res = res + '\n'
return res

def parse_yaml(data):
return yaml.load(data)
Expand Down
31 changes: 14 additions & 17 deletions test/TestUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,50 +203,47 @@ def test_varReplace_nested_list(self):

assert res == 'hello world'

def test_template_varReplace_iterated(self):
template = 'hello $who'
vars = {
'who': 'oh great $person',
'person': 'one',
}

res = ansible.utils.template(template, vars)

assert res == u'hello oh great one'

#####################################
### Template function tests

def test_template_basic(self):
template = 'hello {{ who }}'
vars = {
'who': 'world',
}

res = ansible.utils.template(template, vars, {}, no_engine=False)
res = ansible.utils.template_from_file("test", "template-basic", vars, {})

assert res == 'hello world'

def test_template_whitespace(self):
template = 'hello {{ who }}\n'
vars = {
'who': 'world',
}

res = ansible.utils.template(template, vars, {}, no_engine=False)
res = ansible.utils.template_from_file("test", "template-whitespace", vars, {})

assert res == 'hello world\n'

def test_template_unicode(self):
template = 'hello {{ who }}'
vars = {
'who': u'wórld',
}

res = ansible.utils.template(template, vars, {}, no_engine=False)
res = ansible.utils.template_from_file("test", "template-basic", vars, {})

assert res == u'hello wórld'

def test_template_varReplace_iterated(self):
template = 'hello $who'
vars = {
'who': 'oh great $person',
'person': 'one',
}

res = ansible.utils.template(template, vars)

assert res == u'hello oh great one'

#####################################
### key-value parsing

Expand Down
1 change: 1 addition & 0 deletions test/template-basic
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello {{ who }}
1 change: 1 addition & 0 deletions test/template-whitespace
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello {{ who }}