We all have the need to reboot from time to time. Whenever I need to put a reboot into a play, I have a couple of different ways that I usually do this.
First, is to use a role. I call the role below “reboot”, then I just call it in the playbook when I want it to happen. I’ave had this role for a long time, and I don’t recall if there is a reason I chose to do it this way, or just because I was “new” to ansible when I wrote it.
- name: Initiate Server Reboot
shell: sleep 5 && shutdown -r now "Ansible Requested Restart"
async: 1
poll: 0
ignore_errors: true
- name: Wait for server to come back on line.
wait_for_connection:
delay: 10
timeout: 900
register: reboot_result
- name: Report elapsed time for reboot.
debug:
msg: "The system rebooted in {{ reboot_result.elapsed }} seconds."
Aside from that, I also have playbooks that just use the reboot module
- name: Initiate Server Reboot
reboot:
msg: "Ansible Requested Restart"
pre_reboot_delay: 10
reboot_timeout: 900
register: reboot_result
- name: Report elapsed time for reboot.
debug:
msg: "The system rebooted in {{ reboot_result.elapsed }} seconds."
As you may have noticed, I like the report of how long each reboot takes. 😀