晴耕雨読

working in the fields on fine days and reading books on rainy days

Ansibleのrebootモジュールで再起動する

Ansible で対象サーバを再起動したい場合は、ansible.builtin.reboot モジュールを使用します。

ansible.builtin.reboot の使用例

以下は対象サーバは再起動するための設定例です。 ansible.builtin.reboot では reboot_timeout オプションでサーバが再起動するまでの最大待機時間を指定できます。 最大待機時間のデフォルトは600秒 (10分) です。

なお、rebootしても後続の処理を続けることができます。 今回の例ではreboot後の600秒以内にサーバにログインできた場合、後続の処理が実行されます。 今回は確認のために ping していますが、本番では ping は不要です。

---
- hosts: servers
  tasks:
  - name: Reboot
    ansible.builtin.reboot: reboot_timeout=600
    become: true

  - name: Ping Connection
    ansible.builtin.ping:

実行結果:

$ ansible-playbook -i inventory.ini sample-playbook.yml
PLAY [servers] *****************************************************************
TASK [Gathering Facts] *********************************************************
ok: [aws-rhel]
TASK [Reboot] ******************************************************************
changed: [aws-rhel]
TASK [Ping Connection] *********************************************************
ok: [aws-rhel]
PLAY RECAP *********************************************************************
aws-rhel                   : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

以上です。

参考文献