mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 13:22:48 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user