Add now() jinja2 global func for getting the date/time (#43792)

* Add now() jinja2 global func for getting the date/time

* Docs and changelog

* now isn't a lookup, it doesn't need disabled
This commit is contained in:
Matt Martz 2018-12-07 11:25:53 -06:00 committed by GitHub
commit 3a4d476512
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View file

@ -559,6 +559,18 @@ class Templar:
def _fail_lookup(self, name, *args, **kwargs):
raise AnsibleError("The lookup `%s` was found, however lookups were disabled from templating" % name)
def _now_datetime(self, utc=False, fmt=None):
'''jinja2 global function to return current datetime, potentially formatted via strftime'''
if utc:
now = datetime.datetime.utcnow()
else:
now = datetime.datetime.now()
if fmt:
return now.strftime(fmt)
return now
def _query_lookup(self, name, *args, **kwargs):
''' wrapper for lookup, force wantlist true'''
kwargs['wantlist'] = True
@ -669,6 +681,8 @@ class Templar:
t.globals['lookup'] = self._lookup
t.globals['query'] = t.globals['q'] = self._query_lookup
t.globals['now'] = self._now_datetime
t.globals['finalize'] = self._finalize
jvars = AnsibleJ2Vars(self, t.globals)