mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 05:40:23 -07: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:
parent
3d5d0d355f
commit
3a4d476512
3 changed files with 34 additions and 0 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue