Not very pretty, but works I think.
"at time" supports yymmddhhmm as per reference https://www.juniper.net/documentation/us/en/software/junos/cli-reference/topics/ref/command/request-system-reboot.html#request-system-reboot-command__d63561e236
---
- name: Calculate today's date and tomorrow at 2:00 AM
hosts: localhost
gather_facts: false
tasks:
- name: Get today's date
set_fact:
today_date: "{{ lookup('pipe', 'date +%y%m%d') }}"
- name: Get the current epoch time
set_fact:
current_epoch: "{{ lookup('pipe', 'date +%s') | int }}"
- name: Get tomorrow's date
set_fact:
tomorrow_date: "{{ ((current_epoch | int) + 86400 + 7200) }}"
- name: Restart date
set_fact:
restart_date: "{{ '%y%m%d0200' | ansible.builtin.strftime(second=tomorrow_date, utc=true) }}"
- name: Display today's date
debug:
msg: "Today's date is {{ today_date }}"
- name: Display tomorrow's date at 2:00 AM
debug:
msg: "Restart date is {{ restart_date }}"
Results:
PLAY [Calculate today's date and tomorrow at 2:00 AM] *************************************************************************************************************************************
TASK [Get today's date] *********************************************************************************************************************
ok: [localhost]
TASK [Get the current epoch time] ***********************************************************************************************************
ok: [localhost]
TASK [Get tomorrow's date] ******************************************************************************************************************
ok: [localhost]
TASK [Restart date] ***************************************************************************************************************************
ok: [localhost]
TASK [Display today's date] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "Today's date is 240710"
}
TASK [Display tomorrow's date at 2:00 AM] ***********************************************************************************************************************************************
ok: [localhost] => {
"msg": "Restart date is 2407110200"
}
PLAY RECAP **********************************************************************************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Regards
------------------------------
Andy Sharp
------------------------------
Original Message:
Sent: 07-08-2024 09:03
From: DANIEL ZOOK
Subject: Schedule reboot for "tomorrow" in Ansible
I'm building an Ansible playbook to install firmware updates on multiple switches and then schedule the devices to restart at 2 AM the next day.
What's the best way to accomplish the "reboot at 2 AM tomorrow" part?
I found the below article, but it doesn't suggest a way to schedule a reboot using a relative date. If I use "02:00" for the "at" time, I get an error: "reboot failed. Error: RpcError(severity: error, bad_element: None, message: that time is already past.)"
(I don't want to edit the reboot date every time I run the playbook.)
I assume there is a function in Ansible to calculate tomorrow's date, format it, and assign to a variable. However, I'm still a newbie at using Ansible, so I'm hoping there is a simpler/better way.
Use Ansible to Halt, Reboot, or Shut Down Junos Devices
Juniper |
remove preview |
|
Use Ansible to Halt, Reboot, or Shut Down Junos Devices |
SUMMARY Use the Juniper Networks Ansible modules to halt, reboot, or shut down Junos devices. |
View this on Juniper > |
|
|
Thanks in advance for any advice or suggestions.
------------------------------
djz
------------------------------