-
-
Save infernix/a968f23c4f4e1d6723e4 to your computer and use it in GitHub Desktop.
--- | |
- name: Reboot a host and wait for it to return | |
hosts: somehost | |
remote_user: root | |
tasks: | |
# Send the reboot command | |
- shell: shutdown -r now | |
# This pause is mandatory, otherwise the existing control connection gets reused! | |
- pause: seconds=30 | |
# Now we will run a local 'ansible -m ping' on this host until it returns. | |
# This works with the existing ansible hosts inventory and so any custom ansible_ssh_hosts definitions are being used | |
- local_action: shell ansible -u {{ ansible_user_id }} -m ping {{ inventory_hostname }} | |
register: result | |
until: result.rc == 0 | |
retries: 30 | |
delay: 10 | |
# And finally, execute 'uptime' when the host is back. | |
- shell: uptime |
Does this still work if you have optimised the ansible config to increase the control connection timeout? The default is 60s I believe, but usually I increase it to 30 minutes to speed up the script application.
it will be fine if system is static IP, but, in DHCP, IP address may get change
perfect solution for my case :) thank you very much
Hi, I have a situation where I'm installing an application package in remote server and system automatically reboot as a part of installation process as opposed to control reboot. I'm using command module to install application package. Tried to use pause, wait_for, wait_for_connection nothing works. Wondering if you have any suggestions.
From Ansible 2.7 you can use reboot
module https://docs.ansible.com/ansible/latest/modules/reboot_module.html
wait_for_connection
works better than the local command, and using shell: "(sleep 2 && shutdown -r now) &"
it makes sure that ansible doesnt bail out due to ssh connection dying
I am running ansible version 2.9.6 and the above playbook yaml is giving errors - probably because of the version. I am trying to do a reboot of multiple servers. I've read documentation but am still getting errors on the syntax:
- name: Reboot servers
- hosts: all
- remote_user: root
tasks: - shell: /sbin/reboot
Running a --check is giving me a syntax error on the line tasks. It is hard to tell whether every line needs a - in front or whether I have this in the wrong order. I am going into this completely blinding and haven't used ansible before. Thanks for any help.
I am running ansible version 2.9.6 and the above playbook yaml is giving errors - probably because of the version. I am trying to do a reboot of multiple servers. I've read documentation but am still getting errors on the syntax:
* name: Reboot servers * hosts: all * remote_user: root tasks: * shell: /sbin/reboot
Running a --check is giving me a syntax error on the line tasks. It is hard to tell whether every line needs a - in front or whether I have this in the wrong order. I am going into this completely blinding and haven't used ansible before. Thanks for any help.
Please use a code block to show your code, because indentation matters and it got removed by github
Sorry the * should be a hyphen and I have one space between the hyphen and the name. I also have one space between the colon and the description. I will google how to use a code block meanwhile.
This code is outdated; see comments for better options, most notablly https://docs.ansible.com/ansible/latest/collections/ansible/builtin/reboot_module.html
Thanks, yes I did see that. Based on that, I tried:
- hosts: all
- name: rebooting servers
reboot:
And I get this:
ERROR! Syntax Error while loading YAML.
did not find expected '-' indicator
The offending line appears to be:
- name: rebooting servers
reboot:
^ here
I apologize in advance if my block code above doesn't work
tasks:
- name: Reboot the server
reboot:
- pause:
prompt: "Need delay for restart"
seconds: 30
Thank you for posting this solution. This works better than wait_for when rebooting servers in parallel.
David