Add a new filter: strftime. Use the well known function to format a date output. (#23832)
(cherry picked from commit 3f5b304fc2
)
rebased for @yannig
This commit is contained in:
parent
8517fbf936
commit
3358abcf49
2 changed files with 33 additions and 1 deletions
|
@ -644,6 +644,24 @@ To always exhaust all list use ``zip_longest``::
|
||||||
debug: msg="{{ [1,2,3]|zip_longest(['a','b','c','d','e','f'], [21, 22, 23], fillvalue='X')|list }}"
|
debug: msg="{{ [1,2,3]|zip_longest(['a','b','c','d','e','f'], [21, 22, 23], fillvalue='X')|list }}"
|
||||||
|
|
||||||
|
|
||||||
|
.. versionadded:: 2.4
|
||||||
|
To format a date using a string (like with the shell date command), use the "strftime" filter::
|
||||||
|
|
||||||
|
# Display year-month-day
|
||||||
|
{{ '%Y-%m-%d' | strftime }}
|
||||||
|
|
||||||
|
# Display hour:min:sec
|
||||||
|
{{ '%H:%M:%S' | strftime }}
|
||||||
|
|
||||||
|
# Use ansible_date_time.epoch fact
|
||||||
|
{{ '%Y-%m-%d %H:%M:%S' | strftime(ansible_date_time.epoch) }}
|
||||||
|
|
||||||
|
# Use arbitrary epoch value
|
||||||
|
{{ '%Y-%m-%d' | strftime(0) }} # => 1970-01-01
|
||||||
|
{{ '%Y-%m-%d' | strftime(1441357287) }} # => 2015-09-04
|
||||||
|
|
||||||
|
.. note:: To get all string possibilities, check https://docs.python.org/2/library/time.html#time.strftime
|
||||||
|
|
||||||
Debugging Filters
|
Debugging Filters
|
||||||
`````````````````
|
`````````````````
|
||||||
|
|
||||||
|
|
|
@ -30,13 +30,15 @@ import os.path
|
||||||
import re
|
import re
|
||||||
import string
|
import string
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
import yaml
|
||||||
|
|
||||||
from collections import MutableMapping, MutableSequence
|
from collections import MutableMapping, MutableSequence
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from random import Random, SystemRandom, shuffle
|
from random import Random, SystemRandom, shuffle
|
||||||
|
|
||||||
import yaml
|
|
||||||
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
|
from jinja2.filters import environmentfilter, do_groupby as _do_groupby
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -121,6 +123,15 @@ def to_datetime(string, format="%Y-%d-%m %H:%M:%S"):
|
||||||
return datetime.strptime(string, format)
|
return datetime.strptime(string, format)
|
||||||
|
|
||||||
|
|
||||||
|
def strftime(string_format, second = None):
|
||||||
|
''' return a date string using string. See https://docs.python.org/2/library/time.html#time.strftime for format '''
|
||||||
|
if second is not None:
|
||||||
|
try:
|
||||||
|
second = int(second)
|
||||||
|
except:
|
||||||
|
raise errors.AnsibleFilterError('Invalid value for epoch value (%s)' % second)
|
||||||
|
return time.strftime(string_format, time.localtime(second))
|
||||||
|
|
||||||
def quote(a):
|
def quote(a):
|
||||||
''' return its argument quoted for shell usage '''
|
''' return its argument quoted for shell usage '''
|
||||||
return shlex_quote(a)
|
return shlex_quote(a)
|
||||||
|
@ -510,6 +521,9 @@ class FilterModule(object):
|
||||||
# value as boolean
|
# value as boolean
|
||||||
'bool': to_bool,
|
'bool': to_bool,
|
||||||
|
|
||||||
|
# date formating
|
||||||
|
'strftime': strftime,
|
||||||
|
|
||||||
# quote string for shell usage
|
# quote string for shell usage
|
||||||
'quote': quote,
|
'quote': quote,
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue