I'm trying to restart the server and then wait, using this:
- name: Restart server
shell: reboot
- name: Wait for server to restart
wait_for:
port=22
delay=1
timeout=300
But I get this error:
TASK: [iptables | Wait for server to restart] *********************************
fatal: [example.com] => failed to transfer file to /root/.ansible/tmp/ansible-tmp-1401138291.69-222045017562709/wait_for:
sftp> put /tmp/tmpApPR8k /root/.ansible/tmp/ansible-tmp-1401138291.69-222045017562709/wait_for
Connected to example.com.
Connection closed
2018 Update
As of 2.3, Ansible now ships with the
wait_for_connection
module, which can be used for exactly this purpose.The shutdown -r +1 prevents a return code of 1 to be returned and have ansible fail the task. The shutdown is run as an async task, so we have to delay the
wait_for_connection
task at least 60 seconds. 75 gives us a buffer for those snowflake cases.wait_for_connection - Waits until remote system is reachable/usable
Through trial and error + a lot of reading this is what ultimately worked for me using the 2.0 version of Ansible:
My solution for disabling SELinux and rebooting a node when needed:
Most reliable I've with 1.9.4 got is (this is updated, original version is at the bottom):
Note the
async
option. 1.8 and 2.0 may live with0
but 1.9 wants it1
. The above also checks if machine has actually been rebooted. This is good because once I had a typo that failed reboot and no indication of the failure.The big issue is waiting for machine to be up. This version just sits there for 330 seconds and never tries to access host earlier. Some other answers suggest using port 22. This is good if both of these are true:
These are not always true so I decided to waste 5 minutes compute time.. I hope ansible extend the wait_for module to actually check host state to avoid wasting time.
btw the answer suggesting to use handlers is nice. +1 for handlers from me (and I updated answer to use handlers).
Here's original version but it it not so good and not so reliable:
I've created a reboot_server ansible role that can get dynamically called from other roles with:
The role content is:
This was originally designed to work with Ubuntu OS.
In case you don't have DNS setup for the remote server yet, you can pass the IP address instead of a variable hostname:
These are the two tasks I added to the end of my ansible-swap playbook (to install 4GB of swap on new Digital Ocean droplets.