Ansbile reboot task
I recently answered a question on serverfault about rebooting a host and executing tasks afterwards with Ansible. I got this from a support article which isn’t available anymore. So I guess it might be helpful to share this again here and as a Gist.
- name: "reboot hosts"
shell: "sleep 2 && shutdown -r now 'Reboot triggered by Ansible'" # sleep 2 is needed, else this task might fail
async: "1" # run asynchronously
poll: "0" # don't ask for the status of the command, just fire and forget
ignore_errors: yes # this command will get cut off by the reboot, so ignore errors
- name: "wait for hosts to come up again"
wait_for:
host: ""
port: "22" # wait for ssh as this is what is needed for ansible
state: "started"
delay: "120" # start checking after this amount of time
timeout: "360" # give up after this amount of time
delegate_to: "localhost" # check from the machine executing the playbook