Ansibleによるrebootは、rebootモジュールが使用可能


Ansible rebootモジュールを使用する例 (New in version 2.7)

tasks
- reboot:                   # rebootする

- shell: cat /etc/motd
  register: r
- debug:
    var: r.stdout_lines

公式: reboot – Reboot a machine

TASK [reboot] ******************************************************************************************************************
Friday 13 March 2020  14:42:11 +0900 (0:00:00.126)       0:00:00.126 ********** 
changed: [52.117.38.34]

TASK [shell] *******************************************************************************************************************
Friday 13 March 2020  14:44:47 +0900 (0:02:35.306)       0:02:35.432 ********** 
changed: [52.117.38.34]

TASK [debug] *******************************************************************************************************************
Friday 13 March 2020  14:44:51 +0900 (0:00:04.045)       0:02:39.478 ********** 
ok: [52.117.38.34] => {
    "r.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}

...

=============================================================================== 
reboot ---------------------------------------------------------------------------------------------------------------- 155.31s
shell ------------------------------------------------------------------------------------------------------------------- 4.05s
debug ------------------------------------------------------------------------------------------------------------------- 0.90s

時刻表示の為にansible.cfgの[defaults]にcallback_whitelist = profile_tasksを記載。
自動で再接続まで待ってくれる。

shutdownとwait_for_connectionによる例

tasks
- name: shutdown -Fr &
  shell: shutdown -Fr &     # reboot
  poll: 0                   # 非同期ですぐに次のタスクに進む

- wait_for_connection:      # 接続の回復を待つ
    delay: 30         # ポーリングを開始する前に待機する秒数

- name: cat /etc/motd
  shell: cat /etc/motd
  register: r
- debug:
    var: r.stdout_lines

後続処理を行う為には、wait_for_connectionにて接続回復を待つ必要がある。

TASK [shutdown -Fr &] **********************************************************************************************************
Friday 13 March 2020  15:15:58 +0900 (0:00:00.080)       0:00:00.080 ********** 
changed: [52.117.38.34]

TASK [wait_for_connection] *****************************************************************************************************
Friday 13 March 2020  15:16:05 +0900 (0:00:07.170)       0:00:07.251 ********** 
ok: [52.117.38.34]

TASK [cat /etc/motd] ***********************************************************************************************************
Friday 13 March 2020  15:18:15 +0900 (0:02:09.771)       0:02:17.022 ********** 
changed: [52.117.38.34]

TASK [debug] *******************************************************************************************************************
Friday 13 March 2020  15:18:18 +0900 (0:00:03.191)       0:02:20.214 ********** 
ok: [52.117.38.34] => {
    "r.stdout_lines": [
        "*******************************************************************************",
        "*                                                                             *",
        "*                                                                             *",
        "*  Welcome to AIX Version 7.2!                                                *",
        "*                                                                             *",
        "*                                                                             *",
        "*  Please see the README file in /usr/lpp/bos for information pertinent to    *",
        "*  this release of the AIX Operating System.                                  *",
        "*                                                                             *",
        "*                                                                             *",
        "*******************************************************************************"
    ]
}

...

=============================================================================== 
wait_for_connection --------------------------------------------------------------------------------------------------- 129.77s
shutdown -Fr & ---------------------------------------------------------------------------------------------------------- 7.17s
cat /etc/motd ----------------------------------------------------------------------------------------------------------- 3.19s
debug ------------------------------------------------------------------------------------------------------------------- 0.90s