Jeff Geerling shares a handy tip on how to implement the configure-reboot-configure pattern in an Ansible playbook.
--- - name: Do something that requires a reboot when it results in a change. ... register: task_result - name: Reboot immediately if there was a change. shell: "sleep 5 && reboot" async: 1 poll: 0 when: task_result is changed - name: Wait for the reboot to complete if there was a change. wait_for_connection: connect_timeout: 20 sleep: 5 delay: 5 timeout: 300 when: task_result is changed ...
While looks good, I am using a bit more extended version:
– pause:
prompt: Should we reboot? Press ‘y’ to allow
register: state_user_prompt
when: not reboot_allowed
– name: Reboot the system
shell: sleep 2 && shutdown -r now “Ansible triggered reboot”
async: 1
poll: 0
ignore_errors: true
when: reboot_allowed or (state_user_prompt and state_user_prompt.user_input == ‘y’)
– name: Wait for system to come back
wait_for_connection:
delay: 15
timeout: 300
when: reboot_allowed or (state_user_prompt and state_user_prompt.user_input == ‘y’)
First of all I have an inventory var “reboot_allowed” which is false by default, so that I don’t accidentally reboot something I don’t need. But during the run, if reboot is disabled, ansible will interactivly prompt me if I still want to do it.
The second difference is that instead of plain “reboot” command, I am using “shutdown -r now” with a comment, that you can then see in the logs :-)