ansible.builtin.strftime filter – Returns date and/or time

Note

This filter plugin is part of ansible-core and included in all Ansible installations. In most cases, you can use the short plugin name strftime. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.strftime for easy linking to the plugin documentation and to avoid conflicting with other collections that may have the same filter plugin name.

Synopsis

  • Using Python’s strftime function, take a data formatting string and a date/time to create a formatted date.

Input

This describes the input of the filter, the value before | ansible.builtin.strftime.

Parameter

Comments

Input

string / required

A formatting string following strftime conventions.

Some additional directives are not supported by all platforms. See Python documentation for more details.

See the Python documentation for a reference.

Positional parameters

This describes positional parameters of the filter. These are the values positional1, positional2 and so on in the following example: input | ansible.builtin.strftime(positional1, positional2, ...)

Parameter

Comments

second

integer

A datetime in seconds from epoch to format.

If not supplied, the current time based on time.time() will be used.

utc

boolean

added in ansible-core 2.14

Whether time supplied is in UTC.

If utc is true, the time will be in UTC, otherwise it will be in the local timezone.

Before Ansible Core 2.21, this was based upon time.strftime Python API.

Choices:

  • false ← (default)

  • true

Notes

Note

  • This is a passthrough to Python’s datetime.datetime.strftime, for a complete set of formatting options go to the Python documentation.

  • Before Ansible Core 2.21, this was based upon time.strftime Python API.

Examples

# for a complete set of features go to https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

# Display year-month-day
{{ '%Y-%m-%d' | strftime }}
# => "2021-03-19"

# Display hour:min:sec
{{ '%H:%M:%S' | strftime }}
# => "21:51:04"

# Use ansible_date_time.epoch fact
{{ '%Y-%m-%d %H:%M:%S' | strftime(ansible_date_time.epoch) }}
# => "2021-03-19 21:54:09"

# Use arbitrary epoch value
{{ '%Y-%m-%d' | strftime(0) }}          # => 1970-01-01
{{ '%Y-%m-%d' | strftime(seconds=1441357287, utc=true) }} # => 2015-09-04

Return Value

Key

Description

Return value

string

A formatted date/time string.

Returned: success

Hint

Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.