1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #1195 from jpmens/jtempl

Feature: file info on templates accessible to Jinja2 templates
This commit is contained in:
Michael DeHaan 2012-10-02 05:18:27 -07:00
commit 4677699500

View file

@ -36,6 +36,8 @@ import stat
import termios
import tty
from multiprocessing import Manager
import datetime
import pwd
VERBOSITY=0
@ -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'