I have these 3 tasks:
- name: Obtain facts
ansible.builtin.setup:
- name: Get date time
ansible.builtin.set_fact:
current_date: "{{ ansible_facts.date_time.date | strftime('%d-%m-%Y') }}"
- name: Set timing for maintenance window
ansible.builtin.set_fact:
maintenance_start: "{{ ansible_facts.date_time.iso8601 | strftime('%Y-%m-%dT%H:%M:%S') }}"
maintenance_end: "{{ (ansible_facts.date_time.epoch | int + 3600) | to_datetime('%s') | strftime('%Y-%m-%dT%H:%M:%S') }}"
I have 2 main goals:
dd-mm-YY
2025-02-21T10:33:00
and after that add 1 more hour to the var in maintenance_end
I have this in my ansible_facts
{
"ansible_facts": {
"date_time": {
"year": "2025",
"month": "02",
"weekday": "Wednesday",
"weekday_number": "3",
"weeknumber": "07",
"day": "19",
"hour": "11",
"minute": "51",
"second": "28",
"epoch": "1739976688",
"epoch_int": "1739976688",
"date": "2025-02-19",
"time": "11:51:28",
"iso8601_micro": "2025-02-19T14:51:28.648606Z",
"iso8601": "2025-02-19T14:51:28Z",
"iso8601_basic": "20250219T115128648606",
"iso8601_basic_short": "20250219T115128",
"tz": "-03",
"tz_dst": "-03",
"tz_offset": "-0300"
},
}
}
But I'm reciving this error:
TASK [my-playbook : Get date time] *********************************
task path: /runner/project/my-playbook/tasks/04-maintenance_window.yml:2
fatal: [host1]: FAILED! => {"msg": "Invalid value for epoch value (%d-%m-%Y)"}
fatal: [host2]: FAILED! => {"msg": "Invalid value for epoch value (%d-%m-%Y)"}
fatal: [host3]: FAILED! => {"msg": "Invalid value for epoch value (%d-%m-%Y)"}
Any hits or clue of what is the error?
Q: "Obtaining the current date in the format dd-mm-YY
"
A: That is almost or already available (somehow) within ansible_facts.date_time
.
- name: Get date time
ansible.builtin.set_fact:
current_date: "{{ ansible_facts.date_time.day ~ '-' ~ ansible_facts.date_time.month ~ '-' ~ ansible_facts.date_time.year }}"
or
- name: Get date time
ansible.builtin.set_fact:
current_date: "{{ date_elements | join('-') }}"
vars:
date_elements:
- "{{ ansible_facts.date_time.day }}"
- "{{ ansible_facts.date_time.month }}"
- "{{ ansible_facts.date_time.year }}"
Q: "Obtaining the current date and time in the following format 2025-02-21T10:33:00
and after that add 1 more hour to the var in maintenance_end
"
A: A minimal example playbook
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- 'date_time'
- '!min'
tasks:
- debug:
var: ansible_facts
- debug:
msg: |
"maintenance_start: {{ '%Y-%m-%dT%H:%M:%S' | strftime(ansible_facts.date_time.epoch) }}"
"maintenance_end: {{ '%Y-%m-%dT%H:%M:%S' | strftime(ansible_facts.date_time.epoch | int + 3600) }}"
will result into an output of
TASK [debug] *******************************
ok: [localhost] =>
msg: |-
"maintenance_start: 2025-02-21T18:45:00"
"maintenance_end: 2025-02-21T19:45:00"