Reboot with Ansible raw ᕕ( ᐛ )ᕗ
How to reboot a server with Ansible when Python is not available and you have to use the raw command.
You would think rebooting a machine would be easy? Me too, but damn did this take time 😅
The problem was that I did not have Python on the target system. Meaning I can not use the reboot module. The only thing available was the raw module.
It is even called the "dirty command". What could go wrong?
But before we begin, I need to introduce you to Gary: ᕕ( ᐛ )ᕗ
I stumbled across this one during my research and he will help us do the reboots.
I tried several things and combinations with sleep
which did not work. Then I came across nohup reboot &>/dev/null & exit
:
- name: Going for a reboot ᕕ( ᐛ )ᕗ
raw: ssh -o StrictHostKeyChecking=no root@{{ ansible_host }} "nohup reboot &>/dev/null & exit"
delegate_to: localhost
This worked fine... mostly. Some machines weren't rebooting which I could not understand and do not understand to this day.
After some more testing, I thought about running the reboot command as an on-time systemd-timer
. And systemd
can actually do this with systemd-run --on-active=3
. Neat! And here is the solution which worked as far as I can tell for now.
- name: Going for a reboot ᕕ( ᐛ )ᕗ
raw: systemd-run --on-active=3 shutdown -r now
- name: Pause
pause:
seconds: 5
- name: Wait for SSH
register: result
raw: systemctl is-active system-sshd.slice
until: result.stdout.find("active") != -1
retries: 30
delay: 5
Hope this saves you some time. Man, I have wasted waaaay too much time and energy for a simple reboot.
Have a great day!