I\'m trying to reboot server running CentOS 7
on VirtualBox. I use this task:
- name: Restart server
command: /sbin/reboot
async: 0
poll:
Another solution:
- name: reboot host
command: /usr/bin/systemd-run --on-active=10 /usr/bin/systemctl reboot
async: 0
poll: 0
- name: wait for host sshd
local_action: wait_for host="{{ inventory_hostname }}" search_regex=OpenSSH port=22 timeout=300 delay=30
systemd-run
creates "on the fly" new service which will start systemctl reboot
after 10 sec of delay (--on-active=10
).
delay=30
in wait_for
to add extra 20 sec to be sure that host actually started rebooting.
You're likely not doing anything truly wrong, it's just that /sbin/reboot is shutting down the server so quickly that the server is tearing down the SSH connection used by Ansible before Ansible itself can close it. As a result Ansible is reporting an error because it sees the SSH connection failing for an unexpected reason.
What you might want to do to get around this is to switch from using /sbin/reboot
to using /sbin/shutdown
instead. The shutdown command lets you pass a time, and when combined with the -r
switch it will perform a reboot rather than actually shutting down. So you might want to try a task like this:
- name: Restart server
command: /sbin/shutdown -r +1
async: 0
poll: 0
ignore_errors: true
This will delay the server reboot for 1 minute, but in doing so it should give Ansible enough time to to close the SSH connection itself, thereby avoiding the error that you're currently getting.
- name: restart server
shell: sleep 2 && shutdown -r now "Ansible updates triggered"
async: 1
poll: 0
become: true
ignore_errors: true
- name: waiting for the server to come back
local_action: wait_for host=testcentos state=started delay=30 timeout=300
sudo: false
None of the above solutions worked reliably for me.
Issuing a /sbin/reboot
crashes the play (the SSH connection is closed before ansible finished the task, it crashes even with ignore_errors: true
) and /usr/bin/systemd-run --on-active=2 /usr/bin/systemctl reboot
will not reboot after 2 seconds, but after a random amount of time between 20 seconds and one minute, so the delay is sometime not sufficient and this is not predictable.
Also I don't want to wait for minutes while a cloud server can reboot in few seconds.
So here is my solution:
- name: Reboot the server for kernel update
shell: ( sleep 3 && /sbin/reboot & )
async: 0
poll: 0
- name: Wait for the server to reboot
local_action: wait_for host="{{ansible_host}}" delay=15 state=started port="{{ansible_port}}" connect_timeout=10 timeout=180
That's the shell: ( sleep 3 && /sbin/reboot & )
line that does the trick.
Using ( command & )
in shell script runs a program in the background and detaches it: the command succeed immediately but persists after the shell is destroyed.
Ansible get its response immediately and the server reboots 3 seconds later.
Yet another (combined from other answers) version:
---
- name: restart server
command: /usr/bin/systemd-run --on-active=5 --timer-property=AccuracySec=100ms /usr/bin/systemctl reboot
async: 0
poll: 0
ignore_errors: true
become: yes
- name: wait for server {{ ansible_ssh_host | default(inventory_hostname) }} to come back online
wait_for:
port: 22
state: started
host: '{{ ansible_ssh_host | default(inventory_hostname) }}'
delay: 30
delegate_to: localhost
I am using Ansible 2.5.3. Below code works with ease,
- name: Rebooting host
shell: 'shutdown -r +1 "Reboot triggered by Ansible"'
- wait_for_connection:
delay: 90
timeout: 300
You can reboot immediately, then insert a delay if your machine takes a while to go down:
- name: Rebooting host
shell: 'shutdown -r now "Reboot triggered by Ansible"'
async: 1
poll: 1
ignore_errors: true
# Wait 120 seconds to make sure the machine won't connect immediately in the next section.
- name: Delay for the host to go down
local_action: shell /bin/sleep 120
Then poll to make the playbook return as soon as possible:
- name: Wait for the server to finish rebooting
wait_for_connection:
delay: 15
sleep: 15
timeout: 300
This will make the playbook return as soon as possible after the reboot.