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

Feature: file info on templates accessible to Jinja2 templates #1195

Merged
merged 1 commit into from
Oct 2, 2012
Merged
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
17 changes: 16 additions & 1 deletion lib/ansible/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import termios
import tty
from multiprocessing import Manager
import datetime
import pwd

VERBOSITY=0

Expand Down Expand Up @@ -389,14 +391,27 @@ def template(basedir, text, vars):
def template_from_file(basedir, path, vars):
''' run a file through the templating engine '''

realpath = path_dwim(basedir, path)
environment = jinja2.Environment(loader=jinja2.FileSystemLoader(basedir), trim_blocks=True)
environment.filters['to_json'] = json.dumps
environment.filters['from_json'] = json.loads
environment.filters['to_yaml'] = yaml.dump
environment.filters['from_yaml'] = yaml.load
data = codecs.open(path_dwim(basedir, path), encoding="utf8").read()
data = codecs.open(realpath, encoding="utf8").read()
t = environment.from_string(data)
vars = vars.copy()
try:
template_uid = pwd.getpwuid(os.stat(realpath).st_uid).pw_name
except:
template_uid = os.stat(realpath).st_uid
vars['template_host'] = os.uname()[1]
vars['template_path'] = realpath
vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(realpath))
vars['template_uid'] = template_uid
vars['ansible_managed'] = "%s on %s, modified %s by %s" % (
vars['template_path'], vars['template_host'], vars['template_mtime'],
vars['template_uid'] )

res = t.render(vars)
if data.endswith('\n') and not res.endswith('\n'):
res = res + '\n'
Expand Down