Ansible strftime Filter: Format Dates and Timestamps in Playbooks
By Luca Berton · Published 2024-01-01 · Category: web-servers
Complete guide to Ansible strftime filter. Format dates, timestamps, and ansible_date_time facts with custom patterns. Create backup filenames and log entries.
What Is the strftime Filter?
The strftime filter formats Unix timestamps into human-readable date strings using Python's strftime directives. It's essential for creating timestamped backups, log entries, and dynamic filenames.
See also: ansible_date_time: Format Dates, Time & Timestamps in Playbooks
Basic Usage
---
- name: Format dates with strftime
hosts: localhost
tasks:
- name: Current date formatted
ansible.builtin.debug:
msg: "{{ '%Y-%m-%d' | strftime }}"
# Output: 2026-04-25
- name: Current date and time
ansible.builtin.debug:
msg: "{{ '%Y-%m-%d %H:%M:%S' | strftime }}"
# Output: 2026-04-25 14:30:45
- name: Format a specific timestamp
ansible.builtin.debug:
msg: "{{ '%B %d, %Y' | strftime(1745590245) }}"
# Output: April 25, 2026
Common Format Directives
| Directive | Meaning | Example |
|-----------|---------|---------|
| %Y | 4-digit year | 2026 |
| %m | Month (01-12) | 04 |
| %d | Day (01-31) | 25 |
| %H | Hour 24h (00-23) | 14 |
| %M | Minute (00-59) | 30 |
| %S | Second (00-59) | 45 |
| %A | Weekday name | Friday |
| %B | Month name | April |
| %b | Month abbreviation | Apr |
| %I | Hour 12h (01-12) | 02 |
| %p | AM/PM | PM |
| %Z | Timezone name | UTC |
| %j | Day of year (001-366) | 115 |
| %U | Week number (00-53) | 16 |
See also: ansible_date_time: Use Date, Time & Timestamps in Ansible Playbooks
Practical Examples
Timestamped Backup Filenames
- name: Create timestamped backup
ansible.builtin.copy:
src: /etc/nginx/nginx.conf
dest: "/backup/nginx.conf.{{ '%Y%m%d_%H%M%S' | strftime }}"
remote_src: true
Log Entries with Timestamps
- name: Write timestamped log entry
ansible.builtin.lineinfile:
path: /var/log/ansible-deploy.log
line: "[{{ '%Y-%m-%d %H:%M:%S' | strftime }}] Deployment completed by {{ ansible_user }}"
create: true
Archive with Date
- name: Create dated archive
ansible.builtin.archive:
path: /var/www/html
dest: "/backup/website-{{ '%Y%m%d' | strftime }}.tar.gz"
format: gz
Using ansible_date_time Facts
The ansible_date_time fact provides pre-formatted date/time values:
- name: Show ansible_date_time facts
ansible.builtin.debug:
msg:
- "Date: {{ ansible_date_time.date }}" # 2026-04-25
- "Time: {{ ansible_date_time.time }}" # 14:30:45
- "ISO8601: {{ ansible_date_time.iso8601 }}" # 2026-04-25T14:30:45Z
- "Epoch: {{ ansible_date_time.epoch }}" # 1745590245
- "Weekday: {{ ansible_date_time.weekday }}" # Friday
- "Day: {{ ansible_date_time.day }}" # 25
- "Month: {{ ansible_date_time.month }}" # 04
- "Year: {{ ansible_date_time.year }}" # 2026
Format ansible_date_time.epoch
- name: Custom format from epoch
ansible.builtin.debug:
msg: "{{ '%A, %B %d %Y at %I:%M %p' | strftime(ansible_date_time.epoch) }}"
# Output: Friday, April 25 2026 at 02:30 PM
Calculate Future/Past Dates
- name: Date 7 days from now
ansible.builtin.debug:
msg: "{{ '%Y-%m-%d' | strftime(ansible_date_time.epoch | int + 604800) }}"
# 604800 = 7 days in seconds
- name: Date 30 days ago
ansible.builtin.debug:
msg: "{{ '%Y-%m-%d' | strftime(ansible_date_time.epoch | int - 2592000) }}"
Conditional Based on Time
- name: Run only during business hours
ansible.builtin.debug:
msg: "Running during business hours"
when: ansible_date_time.hour | int >= 9 and ansible_date_time.hour | int < 17
- name: Run only on weekdays
ansible.builtin.debug:
msg: "Running on weekday"
when: ansible_date_time.weekday not in ['Saturday', 'Sunday']
Ansible now() Function
Since Ansible 2.8, you can use now() for the current time:
- name: Using now() function
ansible.builtin.debug:
msg:
- "{{ now(utc=true).strftime('%Y-%m-%d %H:%M:%S UTC') }}"
- "{{ now().isoformat() }}"
FAQ
What is the difference between strftime and ansible_date_time?
strftime is a filter that formats timestamps with custom patterns. ansible_date_time is a fact gathered at playbook start containing pre-formatted date/time values. Use ansible_date_time for common formats and strftime for custom patterns.
How do I convert a date string to epoch in Ansible?
Use the to_datetime filter: {{ '2026-04-25' | to_datetime('%Y-%m-%d') }}. To get epoch seconds, chain with strftime: {{ ('2026-04-25' | to_datetime('%Y-%m-%d')).strftime('%s') }}.
Why does ansible_date_time show the wrong timezone?
ansible_date_time uses the target host's timezone. Set the TZ environment variable or use now(utc=true) for UTC. For a specific timezone, configure it on the target host with timedatectl set-timezone.
See also: Mastering Time in Ansible: An Introduction to the now() Function
Related Articles
• ansible_date_time Facts Guide • Ansible Jinja2 Filters • Ansible set_fact Module • Ansible Template ModuleCategory: web-servers