I\'m trying to reboot server running CentOS 7
on VirtualBox. I use this task:
- name: Restart server
command: /sbin/reboot
async: 0
poll:
At reboot time all ssh connections are closed. That's why the Ansible task fails. The ignore_errors: true
or failed_when: false
additions are no longer working as of Ansible 1.9.x because handling of ssh connections has changed and a closed connection now is a fatal error which can not be caught during play.
The only way I figured out how to do it is to run a local shell task which then starts a separate ssh connection, which then may fail.
- name: Rebooting
delegate_to: localhost
shell: ssh -S "none" {{ inventory_hostname }} sudo /usr/sbin/reboot"
failed_when: false
changed_when: true
if you're using Ansible version >=2.7, you can use reboot
module as described here
The synopsis of the reboot
module itself:
Reboot a machine, wait for it to go down, come back up, and respond to commands.
In a simple way, you can define a simple task like this:
- name: reboot server
reboot:
But you can add some params like test_command
to test if your server is ready to take further tasks
- name: reboot server
reboot:
test_command: whoami
Hope this helps!
Ansible is developing quickly and the older answers were not working for me.
I found two issues:
It is better to run: nohup bash -c "sleep 2s && shutdown -r now" &
This will launch a shell with the sleep
&& shutdown
, but will not wait for the shell to end due to the last &
. The sleep will give some time for the Ansible task to end before the reboot and the nohup
will guarantee that bash doesn't get killed when the task ends.
wait_for
module is not reliably waiting for the SSH service. It detects the port open, probably open by systemd, but when the next task is run, SSH is still not ready.
If you're using Ansible 2.3+, wait_for_connection works reliably.
The best 'reboot and wait' in my experience (I am using Ansible 2.4) is the following:
- name: Reboot the machine
shell: nohup bash -c "sleep 2s && shutdown -r now" &
- name: Wait for machine to come back
wait_for_connection:
timeout: 240
delay: 20
I've got the nohup command from: https://github.com/keithchambers/microservices-playground/blob/master/playbooks/upgrade-packages.yml
I edited this message to:
Following solution works for me perfect:
- name: Restart machine
shell: "sleep 5 && sudo shutdown -r now"
async: 1
poll: 0
- name: wait for ssh again available.
wait_for_connection:
connect_timeout: 20
sleep: 5
delay: 5
timeout: 300
Sleep is required because ansible requires few second's to wrap up connection. Excelent post about this problem was written here: https://www.jeffgeerling.com/blog/2018/reboot-and-wait-reboot-complete-ansible-playbook
After the reboot task, you should have a local_action
task that waits for the remote host to finish rebooting, otherwise, the ssh connection will be terminated and so is the playbook.
- name: Reboot server
command: /sbin/reboot
- name: Wait for the server to finish rebooting
sudo: no
local_action: wait_for host="{{ inventory_hostname }}" search_regex=OpenSSH port=22 timeout=300
I also wrote a blog post about achieving a similar solution: https://oguya.github.io/linux/2015/02/22/ansible-reboot-servers/