@@ -8,3 +8,6 @@ reboot_wait_for_ssh_close_timeout_seconds: 300 | |||||
reboot_wait_for_ssh_open_timeout_seconds: 300 | reboot_wait_for_ssh_open_timeout_seconds: 300 | ||||
reboot_uptime_max_seconds: "{{ (_current_timestamp | int) - (_reboot_shutdown_timestamp | int) }}" | reboot_uptime_max_seconds: "{{ (_current_timestamp | int) - (_reboot_shutdown_timestamp | int) }}" | ||||
ssh_host_pre_reboot: "{{ ansible_host | default(inventory_hostname) }}" | |||||
ssh_host_post_reboot: "{{ ansible_host | default(inventory_hostname) }}" |
@@ -1,52 +1,13 @@ | |||||
--- | --- | ||||
- name: reboot | |||||
command: | |||||
systemd-run --on-active={{reboot_command_after_seconds}} | |||||
shutdown -r now "Reboot triggered by Ansible script" | |||||
#async: 1 | |||||
#poll: 0 | |||||
#ignore_errors: true | |||||
when: reboot_command | |||||
- name: wait for ssh port to be closed | |||||
wait_for: | |||||
host: "{{ ansible_host | default(inventory_hostname) }}" | |||||
port: 22 | |||||
search_regex: OpenSSH | |||||
timeout: "{{ reboot_wait_for_ssh_close_timeout_seconds }}" | |||||
state: absent | |||||
- import_tasks: pre-reboot.yaml | |||||
vars: | vars: | ||||
ansible_connection: local | |||||
register: _reboot_wait_for_ssh_port_closed | |||||
- name: note the shutdown time | |||||
set_fact: | |||||
_reboot_shutdown_timestamp: "{{ _current_timestamp }}" | |||||
ansible_host: "{{ssh_host_pre_reboot}}" | |||||
ssh_host: "{{ ssh_host_pre_reboot }}" | |||||
- name: pause to let host reboot | |||||
pause: | |||||
seconds: "{{ reboot_pause_seconds }}" | |||||
register: _reboot_pause | |||||
- import_tasks: wait-and-pause.yaml | |||||
- name: wait for ssh port to be open again | |||||
wait_for: | |||||
host: "{{ ansible_host | default(inventory_hostname) }}" | |||||
port: 22 | |||||
search_regex: OpenSSH | |||||
timeout: "{{ reboot_wait_for_ssh_open_timeout_seconds }}" | |||||
state: present | |||||
- import_tasks: post-reboot.yaml | |||||
vars: | vars: | ||||
ansible_connection: local | |||||
register: _reboot_wait_for_ssh_port_open | |||||
- import_role: | |||||
name: ssh_host_key | |||||
- name: try to connect to ssh | |||||
setup: | |||||
- name: check uptime for successful reboot | |||||
set_fact: | |||||
reboot_uptime: "{{ ansible_uptime_seconds }}" | |||||
failed_when: ansible_uptime_seconds > reboot_uptime_max_seconds | |||||
ansible_host: "{{ssh_host_post_reboot}}" | |||||
ssh_host: "{{ ssh_host_post_reboot }}" |
@@ -0,0 +1,13 @@ | |||||
--- | |||||
- import_role: | |||||
name: ssh_host_key | |||||
- name: try to connect to ssh | |||||
setup: | |||||
- name: assert realistic uptime for successful reboot | |||||
assert: | |||||
that: (ansible_uptime_seconds | int) <= (reboot_uptime_max_seconds | int) | |||||
msg: reboot might not have happened because of unplausible uptime of {{ansible_uptime_seconds}} seconds is higher than the expected {{reboot_uptime_max_seconds}} seconds | |||||
when: _reboot_shutdown_timestamp is defined |
@@ -0,0 +1,10 @@ | |||||
--- | |||||
- name: reboot | |||||
command: | |||||
systemd-run --on-active={{reboot_command_after_seconds}} | |||||
shutdown -r now "Reboot triggered by Ansible script" | |||||
#async: 1 | |||||
#poll: 0 | |||||
#ignore_errors: true | |||||
when: reboot_command |
@@ -0,0 +1,30 @@ | |||||
--- | |||||
- name: wait for ssh port to be closed | |||||
local_action: wait_for | |||||
args: | |||||
host: "{{ ssh_host_pre_reboot }}" | |||||
port: 22 | |||||
search_regex: OpenSSH | |||||
timeout: "{{ reboot_wait_for_ssh_close_timeout_seconds }}" | |||||
state: absent | |||||
register: _reboot_wait_for_ssh_port_closed | |||||
- name: remember the shutdown time | |||||
set_fact: | |||||
_reboot_shutdown_timestamp: "{{ _current_timestamp }}" | |||||
- name: pause to let host reboot | |||||
pause: | |||||
seconds: "{{ reboot_pause_seconds }}" | |||||
register: _reboot_pause | |||||
- name: wait for ssh port to be open again | |||||
local_action: wait_for | |||||
args: | |||||
host: "{{ ssh_host_post_reboot }}" | |||||
port: 22 | |||||
search_regex: OpenSSH | |||||
timeout: "{{ reboot_wait_for_ssh_open_timeout_seconds }}" | |||||
state: present | |||||
register: _reboot_wait_for_ssh_port_open |