Переглянути джерело

different pre and post hosts and other improvements

master
Markus Katharina Brechtel 2 роки тому
джерело
коміт
6411e97acd
5 змінених файлів з 63 додано та 46 видалено
  1. +3
    -0
      defaults/main.yml
  2. +7
    -46
      tasks/main.yml
  3. +13
    -0
      tasks/post-reboot.yaml
  4. +10
    -0
      tasks/pre-reboot.yaml
  5. +30
    -0
      tasks/wait-and-pause.yaml

+ 3
- 0
defaults/main.yml Переглянути файл

@@ -8,3 +8,6 @@ reboot_wait_for_ssh_close_timeout_seconds: 300
reboot_wait_for_ssh_open_timeout_seconds: 300

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) }}"

+ 7
- 46
tasks/main.yml Переглянути файл

@@ -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:
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:
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 }}"

+ 13
- 0
tasks/post-reboot.yaml Переглянути файл

@@ -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

+ 10
- 0
tasks/pre-reboot.yaml Переглянути файл

@@ -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

+ 30
- 0
tasks/wait-and-pause.yaml Переглянути файл

@@ -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

Завантаження…
Відмінити
Зберегти