With my new job title of Linux System Administrator (more correctly “Administrateur Systèmes Linux“), there are some new things I have had to learn. One is the wonderful world of Ansible.
However, there is one thing that plagued me, rebooting the system and having it wait to run the next command. This is important for patching up a server then making sure everything came back up alright.
After scouring the web and trying out every post I found. I found a post that I was able to piece together other’s response to get a valid, working, solution.
Thank you to gvenka008c and andyhky who’s answers were what i needed to piece together the correct metod.
1 2 3 4 5 6 7 8 9 10 11 |
- name: reboot the server shell: sleep 2 && shutdown -r now async: 1 poll: 0 - name: Wait for server come back wait_for: > host={{ inventory_hostname }} port=22 delay=15 timeout=600 delegate_to: localhost |
If you omit the “sleep 2 &&” The server disconnects before it can run the wait command. If you use “async: 0” (as it describes in many posts online) it disconnects as well. The above worked perfectly for me.
Hope this helps other Ansible users find hope as the net is filled with so many possibilities, not all of them functional or recent.
as of ansible 2.3 you can now use wait_for_connection module which saves you the delegation part of the wait_for
Ansible ensures idempotence, meaning that running a playbook multiple times produces the same result as running it once. This is crucial for maintaining consistent system configurations.